Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit uMain;
- ////////////////////////////////////////////////////////////////////////////////
- /// Visual Program Designer in VCL (Delphi) ///
- /// ------------------------------------------------------------------------ ///
- /// (c) 2016 Dennis Göhlert ///
- /// ///
- /// LICENSE: Mozilla Public License v2.0 ///
- ////////////////////////////////////////////////////////////////////////////////
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, uDesignPrograms,
- Vcl.Grids, Vcl.ValEdit, Vcl.Menus;
- type
- TfmMain = class(TForm)
- pnPrograms: TPanel;
- coPrograms: TComboBox;
- laPrograms: TLabel;
- pbCommands: TPaintBox;
- btAdd: TButton;
- btDelete: TButton;
- pmCommands: TPopupMenu;
- miAdd: TMenuItem;
- miDelete: TMenuItem;
- veCommands: TValueListEditor;
- sbCommands: TScrollBox;
- pnCommands: TPanel;
- btApply: TButton;
- procedure coProgramsDropDown(Sender: TObject);
- procedure btDeleteClick(Sender: TObject);
- procedure btAddClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure pbCommandsPaint(Sender: TObject);
- procedure coProgramsChange(Sender: TObject);
- procedure miAddClick(Sender: TObject);
- procedure miDeleteClick(Sender: TObject);
- procedure pmCommandsPopup(Sender: TObject);
- procedure pbCommandsMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure sbCommandsMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure pbCommandsMouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure pbCommandsMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- procedure btApplyClick(Sender: TObject);
- private
- { Private-Deklarationen }
- Dragging: Boolean;
- DragPos: TPoint;
- Selected: PCommand;
- procedure Select(Command: PCommand);
- public
- { Public-Deklarationen }
- end;
- var
- fmMain: TfmMain;
- implementation
- {$R *.dfm}
- procedure TfmMain.btAddClick(Sender: TObject);
- begin
- SetLength(Programs,Length(Programs) + 1);
- Programs[High(Programs)] := TProgram.Create(InputBox('Add program','Name:','My program'));
- end;
- procedure TfmMain.btDeleteClick(Sender: TObject);
- var
- Index: Integer;
- begin
- Programs[coPrograms.ItemIndex].Free;
- for Index := coPrograms.ItemIndex to High(Programs) - 1 do
- begin
- Programs[Index] := Programs[Index + 1];
- end;
- SetLength(Programs,Length(Programs) - 1);
- coPrograms.ItemIndex := coPrograms.ItemIndex - 1;
- coPrograms.OnChange(coPrograms);
- end;
- procedure TfmMain.btApplyClick(Sender: TObject);
- var
- Command: TCommand;
- begin
- if Assigned(Selected) then
- begin
- Command.ID := StrToInt(veCommands.Values['ID']);
- Command.Duration := StrToInt(veCommands.Values['Duration']);
- Command.Order := StrToInt(veCommands.Values['Order']);
- Programs[coPrograms.ItemIndex].Items[Programs[coPrograms.ItemIndex].IndexOf(Selected^)] := Command;
- Programs[coPrograms.ItemIndex].Sort;
- Select(@Command);
- end;
- end;
- procedure TfmMain.coProgramsChange(Sender: TObject);
- begin
- Select(nil);
- pbCommands.Repaint;
- btDelete.Enabled := coPrograms.ItemIndex <> -1;
- end;
- procedure TfmMain.coProgramsDropDown(Sender: TObject);
- var
- Index: Integer;
- begin
- coPrograms.Items.Clear;
- for Index := Low(Programs) to High(Programs) do
- begin
- coPrograms.Items.Add(Programs[Index].Name);
- end;
- end;
- procedure TfmMain.FormCreate(Sender: TObject);
- begin
- Constraints.MaxHeight := Height;
- Constraints.MinHeight := Height;
- Constraints.MinWidth := Width;
- coPrograms.OnChange(coPrograms);
- end;
- procedure TfmMain.miAddClick(Sender: TObject);
- var
- Command: TCommand;
- begin
- Command.ID := 0;
- Command.Duration := 1;
- Command.Order := Programs[coPrograms.ItemIndex].Count;
- Programs[coPrograms.ItemIndex].Add(Command);
- pbCommands.Repaint;
- end;
- procedure TfmMain.miDeleteClick(Sender: TObject);
- begin
- Programs[coPrograms.ItemIndex].Delete(Programs[coPrograms.ItemIndex].IndexOf(Selected^));
- pbCommands.Repaint;
- end;
- procedure TfmMain.pbCommandsMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- var
- Index: Integer;
- Command: TCommand;
- begin
- if (coPrograms.ItemIndex <> -1) and (Button = mbLeft) then
- begin
- Select(nil);
- if (Y > pbCommands.Height div 2 - 24) and (Y < pbCommands.Height div 2 + 24) then
- begin
- for Index := 0 to Programs[coPrograms.ItemIndex].Count - 1 do
- begin
- if (X > 48 + Index * 66) and (X < 48 + (Index + 1) * 50 + Index * 16) then
- begin
- Command := Programs[coPrograms.ItemIndex].Items[Index];
- Select(@Command);
- Dragging := True;
- DragPos := Point(X,Y);
- end;
- end;
- end;
- end;
- end;
- procedure TfmMain.pbCommandsMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- var
- Index: Integer;
- Command: TCommand;
- begin
- if Dragging then
- begin
- Index := Programs[coPrograms.ItemIndex].IndexOf(Selected^);
- if (Index > 0) and (X < DragPos.X - 16) then
- begin
- Programs[coPrograms.ItemIndex].Exchange(Index,Index - 1);
- Command := Programs[coPrograms.ItemIndex].Items[Index - 1];
- Select(@Command);
- DragPos := Point(X,Y);
- end else
- begin
- if (Index < Programs[coPrograms.ItemIndex].Count - 1) and (X > DragPos.X + 16) then
- begin
- Programs[coPrograms.ItemIndex].Exchange(Index,Index + 1);
- Command := Programs[coPrograms.ItemIndex].Items[Index + 1];
- Select(@Command);
- DragPos := Point(X,Y);
- end;
- end;
- pbCommands.Repaint;
- end;
- end;
- procedure TfmMain.pbCommandsMouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- if Dragging and (Button = mbLeft) then
- begin
- Dragging := False;
- pbCommands.Repaint;
- end;
- end;
- procedure TfmMain.pbCommandsPaint(Sender: TObject);
- var
- Index: Integer;
- begin
- if coPrograms.ItemIndex <> -1 then
- begin
- pbCommands.Width := Programs[coPrograms.ItemIndex].Count * 66 + 80;
- pbCommands.Canvas.Pen.Color := clBlack;
- pbCommands.Canvas.Pen.Width := 4;
- pbCommands.Canvas.MoveTo(24,pbCommands.Height div 2);
- pbCommands.Canvas.LineTo(pbCommands.Width - 24,pbCommands.Height div 2);
- pbCommands.Canvas.Ellipse(16,pbCommands.Height div 2 - 8,32,pbCommands.Height div 2 + 8);
- pbCommands.Canvas.Ellipse(pbCommands.Width - 16,pbCommands.Height div 2 - 8,pbCommands.Width - 32,pbCommands.Height div 2 + 8);
- pbCommands.Canvas.Font.Color := clBlue;
- pbCommands.Canvas.Font.Style := [fsBold];
- pbCommands.Canvas.Font.Size := 8;
- pbCommands.Canvas.TextOut(24 - pbCommands.Canvas.TextWidth('Begin') div 2,pbCommands.Height div 2 + 12,'Begin');
- pbCommands.Canvas.TextOut(pbCommands.Width - 24 - pbCommands.Canvas.TextWidth('End') div 2,pbCommands.Height div 2 + 12,'End');
- Programs[coPrograms.ItemIndex].Sort;
- pbCommands.Canvas.Font.Color := clBlack;
- pbCommands.Canvas.Font.Size := 10;
- pbCommands.Canvas.Pen.Width := 2;
- for Index := 0 to Programs[coPrograms.ItemIndex].Count - 1 do
- begin
- if Assigned(Selected) and (Index = Selected^.Order) then
- begin
- pbCommands.Canvas.Brush.Color := clYellow;
- pbCommands.Canvas.Pen.Color := clGreen;
- end else
- begin
- pbCommands.Canvas.Brush.Color := clRed;
- pbCommands.Canvas.Pen.Color := clBlack;
- end;
- if Assigned(Selected) and Dragging and (Index = Selected^.Order) then
- begin
- pbCommands.Canvas.RoundRect(48 + Index * 66 + pbCommands.ScreenToClient(Mouse.CursorPos).X - DragPos.X,pbCommands.Height div 2 - 24 + pbCommands.ScreenToClient(Mouse.CursorPos).Y - DragPos.Y,48 + (Index + 1) * 50 + Index * 16 + pbCommands.ScreenToClient(Mouse.CursorPos).X - DragPos.X,pbCommands.Height div 2 + 24 + pbCommands.ScreenToClient(Mouse.CursorPos).Y - DragPos.Y,8,8);
- pbCommands.Canvas.TextOut(73 + Index * 66 + pbCommands.ScreenToClient(Mouse.CursorPos).X - DragPos.X - pbCommands.Canvas.TextWidth(IntToStr(Programs[coPrograms.ItemIndex].Items[Index].ID)) div 2,pbCommands.Height div 2 + pbCommands.ScreenToClient(Mouse.CursorPos).Y - DragPos.Y - pbCommands.Canvas.TextHeight(IntToStr(Programs[coPrograms.ItemIndex].Items[Index].ID)) div 2,IntToStr(Programs[coPrograms.ItemIndex].Items[Index].ID));
- end else
- begin
- pbCommands.Canvas.RoundRect(48 + Index * 66,pbCommands.Height div 2 - 24,48 + (Index + 1) * 50 + Index * 16,pbCommands.Height div 2 + 24,8,8);
- pbCommands.Canvas.TextOut(73 + Index * 66 - pbCommands.Canvas.TextWidth(IntToStr(Programs[coPrograms.ItemIndex].Items[Index].ID)) div 2,pbCommands.Height div 2 - pbCommands.Canvas.TextHeight(IntToStr(Programs[coPrograms.ItemIndex].Items[Index].ID)) div 2,IntToStr(Programs[coPrograms.ItemIndex].Items[Index].ID));
- end;
- end;
- end;
- end;
- procedure TfmMain.pmCommandsPopup(Sender: TObject);
- begin
- miAdd.Enabled := coPrograms.ItemIndex <> -1;
- end;
- procedure TfmMain.sbCommandsMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- if Button = mbLeft then
- begin
- Select(nil);
- end;
- end;
- procedure TfmMain.Select(Command: PCommand);
- begin
- Dispose(Selected);
- veCommands.Strings.Clear;
- if Assigned(Command) then
- begin
- New(Selected);
- Selected^ := Command^;
- veCommands.Strings.Add('ID=' + IntToStr(Command^.ID));
- veCommands.Strings.Add('Duration=' + IntToStr(Command^.Duration));
- veCommands.Strings.Add('Order=' + IntToStr(Command^.Order));
- veCommands.Enabled := True;
- veCommands.ItemProps['Order'].ReadOnly := True;
- miDelete.Enabled := True;
- btApply.Enabled := True;
- end else
- begin
- Selected := nil;
- veCommands.Enabled := False;
- miDelete.Enabled := False;
- btApply.Enabled := False;
- end;
- pbCommands.Repaint;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement