Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: So Apr 28, 2024 19:56

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 
Autor Nachricht
 Betreff des Beitrags: Basic GUI Class
BeitragVerfasst: So Jul 12, 2009 15:53 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Iv just completed a basic GUI "Element" (Think TObject of the GUI world) and as it may be useful to others you can get it below. The GUI Element Class supports children and passes events down to them. To use it you just need to create a new class based on the TGUI_Element and add your own code (as in TWhatever = Class(TGUI_Element) ). The class currently doesn't support keyboard input(Tho it wouldn't be hard to add it).

It has the following events:
OnPreRender - Triggered when PreRender is called
OnRender - Triggered when Render is called
OnParentSizeChanged - Triggered when the parents size changed
OnMouseDown - Triggered on MouseDown
OnMouseUp - Triggered on MouseUp
OnMouseMove - Triggered on MouseMove

Note: All the above events except OnParentSizeChanged are not fired if the component is not visible.

Main Unit
Code:
  1. unit FME_GUI_Unit;
  2.  
  3. {
  4.  Started on 17 June 2009
  5.  by Stuart "Stucuk" Carey
  6.  Copyright (C) 2009
  7. }
  8.  
  9. interface
  10.  
  11. type
  12. //This is effectivly a copy of the TShiftState but in record form
  13. TGUI_Mouse_State = Packed Record
  14.  ssShift, ssAlt, ssCtrl, ssLeft,
  15.  ssRight, ssMiddle, ssDouble : Boolean;
  16. end;
  17.  
  18. //This is a copy of TMouseButton
  19. TGUI_Mouse_Button = (GUI_mbLeft, GUI_mbRight, GUI_mbMiddle);
  20.  
  21. TGUI_Element = Class(TObject)
  22. protected
  23.   FParent       : Pointer;
  24.   FWidth,
  25.   FHeight,
  26.   FX,
  27.   FY            : Integer;
  28.   FVisible,
  29.   FNeedUpdate   : Boolean;
  30.   FChildren     : Array of Pointer;
  31.   FMouse_State  : TGUI_Mouse_State;
  32.   FChildDown    : Boolean;
  33.   function FindChild(Child : Pointer) : Integer;
  34.   function FindFreeChild() : Integer;
  35.   function MouseDownInternal(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer) : Boolean;
  36.   procedure MouseUpInternal(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer);
  37.  public
  38.   //Constructor/Destructor
  39.   constructor Create(Parent : Pointer);
  40.   destructor Destroy; Override;
  41.  
  42.   //Element
  43.   procedure AddChild(Child : Pointer);
  44.   procedure RemoveChild(Child : Pointer);
  45.  
  46.   //Render/Update
  47.   procedure PreRender;
  48.   procedure Render;
  49.   procedure Update;
  50.  
  51.   //Set
  52.   procedure SetXY(X,Y : Integer);
  53.   procedure SetWidthHeight(Width,Height : Integer);
  54.   procedure SetVisible(Value : Boolean);
  55.  
  56.   //Get
  57.   function  GetX       : Integer;
  58.   function  GetY       : Integer;
  59.   procedure GetTrueXY(var X,Y : Integer);
  60.   function  GetWidth   : Integer;
  61.   function  GetHeight  : Integer;
  62.   function  GetVisible : Boolean;
  63.  
  64.   //Mouse/Keyboard
  65.   function XYInBounds(X,Y : Integer) : Boolean;
  66.   function MouseDown(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer) : Boolean;
  67.   procedure MouseUp(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer);
  68.   procedure MouseMove(State : TGUI_Mouse_State; X, Y: Integer);
  69.  
  70.   //Events
  71.   procedure OnPreRender; Virtual;
  72.   procedure OnRender; Virtual;
  73.  
  74.   procedure OnParentSizeChanged(WidthDiff,HeightDiff : Integer); Virtual; // Called by the parent
  75.  
  76.   procedure OnMouseDown(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer); Virtual;
  77.   procedure OnMouseUp(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer); Virtual;
  78.   procedure OnMouseMove(State : TGUI_Mouse_State; X, Y: Integer); Virtual;
  79. end;
  80.  
  81. implementation
  82.  
  83. //## Constructor/Destructor ##//
  84.  
  85. constructor TGUI_Element.Create(Parent : Pointer);
  86. begin
  87.  inherited Create;
  88.  SetLength(FChildren,0);
  89.  FParent      := Parent;
  90.  FWidth       := -1;
  91.  FWidth       := -1;
  92.  FX           := -1;
  93.  FY           := -1;
  94.  FVisible     := False;
  95.  FNeedUpdate  := True;
  96.  FillChar(FMouse_State,SizeOf(FMouse_State),0);
  97.  if Assigned(FParent) then
  98.  TGUI_Element(FParent).AddChild(Self);
  99. end;
  100.  
  101. destructor TGUI_Element.Destroy;
  102. var
  103.  X : Integer;
  104. begin
  105.  if High(FChildren) > -1 then
  106.  for X := 0 to High(FChildren) do
  107.  if Assigned(FChildren[X]) then
  108.  begin
  109.   TGUI_Element(FChildren[X]).Free;
  110.   FChildren[X] := Nil;
  111.  end;
  112.  
  113.  SetLength(FChildren,0);
  114.  
  115.  if Assigned(FParent) then
  116.  TGUI_Element(FParent).RemoveChild(Self);
  117. end;
  118.  
  119. //## Element ##//
  120.  
  121. function TGUI_Element.FindChild(Child : Pointer) : Integer;
  122. var
  123.  X : Integer;
  124. begin
  125.  Result := -1;
  126.  if High(FChildren) > -1 then
  127.  for X := 0 to High(FChildren) do
  128.  if Assigned(FChildren[X]) and (Child = FChildren[X]) then
  129.  begin
  130.   Result := X;
  131.   Break;
  132.  end;
  133. end;
  134.  
  135. function TGUI_Element.FindFreeChild() : Integer;
  136. var
  137.  X : Integer;
  138. begin
  139.  Result := -1;
  140.  if High(FChildren) > -1 then
  141.  for X := 0 to High(FChildren) do
  142.  if not Assigned(FChildren[X]) then
  143.  begin
  144.   Result := X;
  145.   Break;
  146.  end;
  147. end;
  148.  
  149. procedure TGUI_Element.AddChild(Child : Pointer);
  150. var
  151.  X : Integer;
  152. begin
  153.  X := FindChild(Child);
  154.  if X > -1 then Exit; //Already a child.
  155.  X := FindFreeChild();
  156.  if X = -1 then
  157.  begin
  158.   SetLength(FChildren,High(FChildren)+2);
  159.   X := High(FChildren);
  160.  end;
  161.  FChildren[X] := Child;
  162. end;
  163.  
  164. procedure TGUI_Element.RemoveChild(Child : Pointer);
  165. var
  166.  X : Integer;
  167. begin
  168.  X := FindChild(Child);
  169.  if X = -1 then Exit;
  170.  FChildren[X] := Nil;
  171. end;
  172.  
  173. //## Render/Update ##//
  174.  
  175. procedure TGUI_Element.OnPreRender;
  176. begin
  177.  //
  178. end;
  179.  
  180. procedure TGUI_Element.PreRender;
  181. var
  182.  X : Integer;
  183. begin
  184.  if not FVisible then Exit;
  185.  OnPreRender;
  186.  if High(FChildren) > -1 then
  187.  for X := 0 to High(FChildren) do
  188.  if Assigned(FChildren[X]) then
  189.  TGUI_Element(FChildren[X]).PreRender;
  190. end;
  191.  
  192. procedure TGUI_Element.OnRender;
  193. begin
  194.  //
  195. end;
  196.  
  197. procedure TGUI_Element.Render;
  198. var
  199.  X : Integer;
  200. begin
  201.  if not FVisible then Exit;
  202.  OnRender;
  203.  FNeedUpdate := False;
  204.  
  205.  if High(FChildren) > -1 then
  206.  for X := 0 to High(FChildren) do
  207.  if Assigned(FChildren[X]) then
  208.  TGUI_Element(FChildren[X]).Render;
  209. end;
  210.  
  211. procedure TGUI_Element.Update;
  212. begin
  213.  FNeedUpdate := True;
  214. end;
  215.  
  216. //## Set ##//
  217.  
  218. procedure TGUI_Element.SetXY(X,Y : Integer);
  219. var
  220.  Z      : Integer;
  221. begin
  222.  FX    := X;
  223.  FY    := Y;
  224.  if High(FChildren) > -1 then
  225.  for Z := 0 to High(FChildren) do
  226.  if Assigned(FChildren[Z]) then
  227.  TGUI_Element(FChildren[Z]).Update;
  228. end;
  229.  
  230. procedure TGUI_Element.SetWidthHeight(Width,Height : Integer);
  231. var
  232.  WidthDiff,
  233.  HeightDiff,
  234.  X           : Integer;
  235. begin
  236.  WidthDiff  := Width-FWidth;
  237.  HeightDiff := Height-FHeight;
  238.  FWidth     := Width;
  239.  FHeight    := Height;
  240.  if High(FChildren) > -1 then
  241.  for X := 0 to High(FChildren) do
  242.  if Assigned(FChildren[X]) then
  243.  begin
  244.   TGUI_Element(FChildren[X]).Update;
  245.   TGUI_Element(FChildren[X]).OnParentSizeChanged(WidthDiff,HeightDiff);
  246.  end;
  247. end;
  248.  
  249. procedure TGUI_Element.SetVisible(Value : Boolean);
  250. begin
  251.  if (FWidth = -1) or (FHeight = -1) or (FX = -1) or (FY = -1) then
  252.  begin
  253.   if FVisible then
  254.   Update;
  255.   FVisible := False;
  256.   exit;
  257.  end;
  258.  
  259.  if FVisible = Value then Exit;
  260.  FVisible := Value;
  261.  Update;
  262. end;
  263.  
  264. //## Get ##//
  265. function TGUI_Element.GetX : Integer;
  266. begin
  267.  Result := FX;
  268. end;
  269.  
  270. function TGUI_Element.GetY : Integer;
  271. begin
  272.  Result := FY;
  273. end;
  274.  
  275. procedure TGUI_Element.GetTrueXY(var X,Y : Integer);
  276. begin
  277.  If Assigned(FParent) then
  278.  TGUI_Element(FParent).GetTrueXY(X,Y)
  279.  else
  280.  begin
  281.   X := 0;
  282.   Y := 0;
  283.  end;
  284.  
  285.  Inc(X,FX);
  286.  Inc(Y,FY);
  287. end;
  288.  
  289. function TGUI_Element.GetWidth : Integer;
  290. begin
  291.  Result := FWidth;
  292. end;
  293.  
  294. function TGUI_Element.GetHeight : Integer;
  295. begin
  296.  Result := FHeight;
  297. end;
  298.  
  299. function TGUI_Element.GetVisible : Boolean;
  300. begin
  301.  Result := FVisible;
  302. end;
  303.  
  304. //## Mouse/Keyboard ##//
  305.  
  306. // Sends the MouseDown to all Children
  307. function TGUI_Element.MouseDownInternal(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer) : Boolean;
  308. var
  309.  Z,LX,LY : Integer;
  310. begin
  311.  Result := False;
  312.  if High(FChildren) > -1 then
  313.  for Z := 0 to High(FChildren) do
  314.  if Assigned(FChildren[Z]) then
  315.  begin
  316.   LX     := X-TGUI_Element(FChildren[Z]).GetX;
  317.   LY     := Y-TGUI_Element(FChildren[Z]).GetY;
  318.   Result := TGUI_Element(FChildren[Z]).MouseDown(Button,State,LX,LY);
  319.   if Result then Break;
  320.  end;
  321. end;
  322.  
  323. procedure TGUI_Element.MouseUpInternal(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer);
  324. var
  325.  Z,LX,LY : Integer;
  326. begin
  327.  if High(FChildren) > -1 then
  328.  for Z := 0 to High(FChildren) do
  329.  if Assigned(FChildren[Z]) then
  330.  begin
  331.   LX     := X-TGUI_Element(FChildren[Z]).GetX;
  332.   LY     := Y-TGUI_Element(FChildren[Z]).GetY;
  333.   TGUI_Element(FChildren[Z]).MouseUp(Button,State,LX,LY);
  334.  end;
  335. end;
  336.  
  337. function TGUI_Element.XYInBounds(X,Y : Integer) : Boolean;
  338. begin
  339.  Result := not ((X < 0) or (X > FWidth) or (Y < 0) or (Y > FHeight));
  340. end;
  341.  
  342. procedure TGUI_Element.OnMouseDown(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer);
  343. begin
  344.  //
  345. end;
  346.  
  347. function TGUI_Element.MouseDown(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer) : Boolean;
  348. begin
  349.  Result := False;
  350.  if not FVisible then exit;
  351.  if Not Assigned(FParent) then
  352.  begin
  353.   X := X-GetX;
  354.   Y := Y-GetY;
  355.  end;
  356.  
  357.  FChildDown := MouseDownInternal(Button, State, X, Y);
  358.  if not FChildDown then
  359.  if XYInBounds(X,Y) then
  360.  begin
  361.   case Button of
  362.    GUI_mbLeft   : FMouse_State.ssLeft   := True;
  363.    GUI_mbMiddle : FMouse_State.ssMiddle := True;
  364.    GUI_mbRight  : FMouse_State.ssRight  := True;
  365.   end;
  366.   Result := True;
  367.  end;
  368.  
  369.  Result := Result or FChildDown;
  370.  OnMouseDown(Button,State,X,Y);
  371. end;
  372.  
  373. procedure TGUI_Element.OnMouseUp(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer);
  374. begin
  375.  //
  376. end;
  377.  
  378. procedure TGUI_Element.MouseUp(Button : TGUI_Mouse_Button; State : TGUI_Mouse_State; X, Y: Integer);
  379. begin
  380.  if Not Assigned(FParent) then
  381.  begin
  382.   X := X-GetX;
  383.   Y := Y-GetY;
  384.  end;
  385.  
  386.  MouseUpInternal(Button, State, X, Y);
  387.   case Button of
  388.    GUI_mbLeft   : FMouse_State.ssLeft   := False;
  389.    GUI_mbMiddle : FMouse_State.ssMiddle := False;
  390.    GUI_mbRight  : FMouse_State.ssRight  := False;
  391.   end;
  392.  
  393.  if FVisible then
  394.  OnMouseUp(Button,State,X,Y);
  395. end;
  396.  
  397. procedure TGUI_Element.OnMouseMove(State : TGUI_Mouse_State; X, Y: Integer);
  398. begin
  399.  //
  400. end;
  401.  
  402. procedure TGUI_Element.MouseMove(State : TGUI_Mouse_State; X, Y: Integer);
  403. var
  404.  Z,LX,LY : Integer;
  405. begin
  406.  if not FVisible then exit;
  407.  
  408.  if Not Assigned(FParent) then
  409.  begin
  410.   X := X-GetX;
  411.   Y := Y-GetY;
  412.  end;
  413.  
  414.  if High(FChildren) > -1 then
  415.  for Z := 0 to High(FChildren) do
  416.  if Assigned(FChildren[Z]) then
  417.  begin
  418.   LX := X-TGUI_Element(FChildren[Z]).GetX;
  419.   LY := Y-TGUI_Element(FChildren[Z]).GetY;
  420.   TGUI_Element(FChildren[Z]).MouseMove(State,LX,LY);
  421.  end;
  422.  
  423.  OnMouseMove(State,X,Y);
  424. end;
  425.  
  426. procedure TGUI_Element.OnParentSizeChanged(WidthDiff,HeightDiff : Integer);
  427. begin
  428.  //
  429. end;
  430.  
  431. end.


Helper Functions
Code:
  1. unit FME_GUI_Helpers;
  2.  
  3. {
  4.  Helper Functions
  5.  
  6.  Started on 12 July 2009
  7.  by Stuart "Stucuk" Carey
  8.  Copyright (C) 2009
  9. }
  10.  
  11. interface
  12.  
  13. uses FME_GUI_Unit,Classes,Controls;
  14.  
  15. function TShiftStateToTGUI_Mouse_State(Shift : TShiftState) : TGUI_Mouse_State;
  16. function TMouseButtonToTGUI_Mouse_Button(Button : TMouseButton) : TGUI_Mouse_Button;
  17.  
  18. implementation
  19.  
  20. function TShiftStateToTGUI_Mouse_State(Shift : TShiftState) : TGUI_Mouse_State;
  21. begin
  22.  Result.ssAlt    := ssAlt in Shift;
  23.  Result.ssCtrl   := ssCtrl in Shift;
  24.  Result.ssDouble := ssDouble in Shift;
  25.  Result.ssLeft   := ssLeft in Shift;
  26.  Result.ssMiddle := ssMiddle in Shift;
  27.  Result.ssRight  := ssRight in Shift;
  28.  Result.ssShift  := ssShift in Shift;
  29. end;
  30.  
  31. function TMouseButtonToTGUI_Mouse_Button(Button : TMouseButton) : TGUI_Mouse_Button;
  32. begin
  33.  case Button of
  34.   mbLeft   : Result := GUI_mbLeft;
  35.   mbMiddle : Result := GUI_mbMiddle;
  36.   mbRight  : Result := GUI_mbRight;
  37.  end;
  38. end;
  39.  
  40. end.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 15 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.028s | 17 Queries | GZIP : On ]