Recent

Author Topic: BGRAScript  (Read 10595 times)

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
BGRAScript
« on: August 13, 2014, 12:41:57 am »
Is now part of BGRA-Controls
http://forum.lazarus.freepascal.org/index.php/topic,24176.0.html

Add a bgragraphiccontrol with redraw event and a SynEdit.

paste this in SynEdit.Lines in the object inspector
Quote
FillTransparent
RectangleAntialias,2,2,98,98,"rgb(100,100,100)","4,5","rgb(255,255,255)"
Rectangle,10,10,90,90,"rgba(100,100,100,100)","rgb(100,0,100,100)"
//,SaveToFile,"file1.png"
DrawHorizLinediff,0,40,200,"rgb(100,100,0)","rgb(255,255,255)",100
SaveToFile,"file2.png"

All the stuff (in Windows you can see the debug if you turn off Project > Settings > Config and target > Win32 ui application -WG)
Code: [Select]
procedure TForm1.BGRAGraphicControl1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
begin
  ScriptCommandList(SynEdit1.Lines, bitmap);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SynCompletionList(SynCompletion1.ItemList);
end;

Press Shift + Space to see the templates.

BGRAScript unit
Code: [Select]
unit BGRAScript;

{$mode objfpc}{$H+}
{$define debug}

interface

uses
  Classes, SysUtils, BGRABitmap, BGRABitmapTypes;

{Template}
procedure SynCompletionList(itemlist: TStrings);
{Scripting}
function ScriptCommand(command: string; bitmap: TBGRABitmap): boolean;
function ScriptCommandList(commandlist: TStrings; bitmap: TBGRABitmap): boolean;

implementation

procedure SynCompletionList(itemlist: TStrings);
begin
  with itemlist do
  begin
    {TFPCustomImage override}
    Add('SetSize,320,240');
    {Loading functions}
    Add('SaveToFile,"file.png"');
    {Loading functions}
    Add('SetHorizLine,0,0,100,"rgba(0,0,0,255)"');
    Add('XorHorizLine,0,0,100,"rgba(0,0,0,255)"');
    Add('DrawHorizLine,0,0,100,"rgba(0,0,0,255)"');
    Add('FastBlendHorizLine,0,0,100,"rgba(0,0,0,255)"');
    Add('AlphaHorizLine,0,0,100,"rgba(0,0,0,255)"');
    Add('SetVertLine,0,0,100,"rgba(0,0,0,255)"');
    Add('XorVertLine,0,0,100,"rgba(0,0,0,255)"');
    Add('DrawVertLine,0,0,100,"rgba(0,0,0,255)"');
    Add('FastBlendVertLine,0,0,100,"rgba(0,0,0,255)"');
    Add('AlphaVertLine,0,0,100,"rgba(0,0,0,255)"');
    Add('DrawHorizLinediff,0,0,100,"rgba(0,0,0,255)","rgba(255,255,255,255)",128');
    //--
    Add('FillTransparent');
    Add('Rectangle');
    Add('RectangleAntiAlias');
  end;
end;

function ScriptCommand(command: string; bitmap: TBGRABitmap): boolean;

  function ParamCheck(passed, mustbe: integer): boolean;
  begin
    Result := True;
    if passed <> mustbe then
      Result := False;

  {$ifdef debug}
    if not Result then
    begin
      writeln('>> Wrong number of parameters: ' + IntToStr(passed));
      writeln('>> Must be: ' + IntToStr(mustbe));
    end;
  {$endif}
  end;

var
  list: TStringList;
  passed: integer;
  {$ifdef debug}
  i: integer;
  {$endif}
begin
  {$ifdef debug}
  writeln('---Script Command---');
  {$endif}

  Result := True;
  list := TStringList.Create;
  list.CommaText := command;
  passed := list.Count;

  case LowerCase(list[0]) of

    {TFPCustomImage override}
    'setsize':
    begin
      Result := ParamCheck(passed, 3);
      if Result then
        bitmap.SetSize(StrToInt(list[1]), StrToInt(list[2]));
    end;

    {Loading functions}
    'savetofile':
    begin
      Result := ParamCheck(passed, 2);
      if Result then
        bitmap.SaveToFile(list[1]);
    end;

    {Pixel functions}

    {Loading functions}
    {* Horiz *}
    'sethorizline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.SetHorizLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]));
    end;
    'xorhorizline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.XorHorizLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]));
    end;
    'drawhorizline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.DrawHorizLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]));
    end;
    'fastblendhorizline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.FastBlendHorizLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]));
    end;
    'alphahorizline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.AlphaHorizLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToInt(list[4]));
    end;
    {* Vert *}
    'setvertline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.SetVertLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]));
    end;
    'xorvertline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.XorVertLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]));
    end;
    'drawvertline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.DrawVertLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]));
    end;
    'fastblendvertline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.FastBlendVertLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]));
    end;
    'alphavertline':
    begin
      Result := ParamCheck(passed, 5);
      if Result then
        bitmap.AlphaVertLine(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToInt(list[4]));
    end;
    {* Misc *}
    'drawhorizlinediff':
    begin
      Result := ParamCheck(passed, 7);
      if Result then
        bitmap.DrawHorizLineDiff(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToBGRA(list[4]), StrToBGRA(list[5]), StrToInt(list[6]));
    end;

    //---
    'filltransparent':
    begin
      Result := ParamCheck(passed, 1);
      if Result then
        bitmap.FillTransparent;
    end;

    'rectangle':
    begin
      Result := ParamCheck(passed, 7);
      if Result then
        bitmap.Rectangle(StrToInt(list[1]), StrToInt(list[2]), StrToInt(
          list[3]), StrToInt(list[4]), StrToBGRA(list[5]), StrToBGRA(list[6]),
          dmDrawWithTransparency);
    end;

    'rectangleantialias':
    begin
      Result := ParamCheck(passed, 8);
      if Result then
        bitmap.RectangleAntialias(StrToInt(list[1]), StrToInt(list[2]),
          StrToInt(list[3]), StrToInt(list[4]), StrToBGRA(list[5]),
          StrToFloat(list[6]), StrToBGRA(list[7]));
    end;

    '//':
    begin
      //,comment
    end

    else
    begin
      Result := False;
    end;
  end;

  {$ifdef debug}
  if not Result then
    writeln('>> ERROR');
  for i := 0 to list.Count - 1 do
    writeln(' ' + list[i]);
  writeln('--------------------');
  {$endif}
end;

function ScriptCommandList(commandlist: TStrings; bitmap: TBGRABitmap): boolean;
var
  i: integer;
begin
  {$ifdef debug}
  writeln('----Script  List----');
  writeln(' Executing ' + IntToStr(commandlist.Count) + ' lines...');
  writeln('--------------------');
  {$endif}

  Result := True;
  for i := 0 to commandlist.Count - 1 do
    if commandlist[i] <> '' then
      ScriptCommand(commandlist[i], bitmap);

  {$ifdef debug}
  writeln('----Script  List----');
  writeln(' END');
  writeln('--------------------');
  {$endif}
end;

end.

« Last Edit: August 15, 2014, 06:52:43 pm by 007 »

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: BGRAScript
« Reply #1 on: August 13, 2014, 06:50:11 am »
That's a cool idea - runtime drawing commands via the memo. Not sure where it would be used, but still a cool idea. :)
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: BGRAScript
« Reply #2 on: August 13, 2014, 07:12:48 am »
That's a cool idea - runtime drawing commands via the memo. Not sure where it would be used, but still a cool idea. :)

svg drawer?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Re: BGRAScript
« Reply #3 on: August 13, 2014, 08:33:15 am »
Nice!  :)
My projects are on Gitlab and on Codeberg.

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: BGRAScript
« Reply #4 on: August 13, 2014, 06:40:41 pm »
In this example is only used with a memo, but for example we can create a command line version via paramstr() generate and save bitmaps.

I will extend it to include more parameters.

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: BGRAScript
« Reply #5 on: August 13, 2014, 07:44:05 pm »
I've added all line primitives and also a verification to ensure that the number of parameters is ok.

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Re: BGRAScript
« Reply #6 on: August 13, 2014, 10:32:16 pm »
Good job!

With the possibility to save pictures, it becomes a useful tool. It would be interesting if (as you suggested) one could drag and drop scripts. It would be a way to experiment easily BGRABitmap functions.  :)
My projects are on Gitlab and on Codeberg.

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: BGRAScript
« Reply #7 on: August 14, 2014, 05:43:54 am »
That's great.

I would suggest to trim the string and retrieve the first parameter using the position of the first space with Pos, so that a command line would look like:
Rectangle 10,10,90,90,"rgba(100,100,100,100)","rgb(100,0,100,100)"

when checking for the function name, you can LowerCase function so that case is ignored.
Conscience is the debugger of the mind

zamtmn

  • Hero Member
  • *****
  • Posts: 594
Re: BGRAScript
« Reply #8 on: August 14, 2014, 10:00:38 am »
Nice! But I think that it is better not to invent a new scripting language, better implement binding to PascalScript. This is a much more powerful and a reliable solution

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: BGRAScript
« Reply #9 on: August 14, 2014, 04:59:35 pm »
svg drawer?
Using Canvas2d functions?

Nice! But I think that it is better not to invent a new scripting language, better implement binding to PascalScript. This is a much more powerful and a reliable solution
One does not exclude the other
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: BGRAScript
« Reply #10 on: August 14, 2014, 11:41:17 pm »
Nice! But I think that it is better not to invent a new scripting language, better implement binding to PascalScript. This is a much more powerful and a reliable solution

No idea how to do that.. If you can do it. Is just CSV, it will be never a scripting language, at least the things I can do.

That's great.

I would suggest to trim the string and retrieve the first parameter using the position of the first space with Pos, so that a command line would look like:
Rectangle 10,10,90,90,"rgba(100,100,100,100)","rgb(100,0,100,100)"

when checking for the function name, you can LowerCase function so that case is ignored.

Thanks. I've added LowerCase. But I prefer to keep it as a CSV file.

I've added a list of templates for SynEdit, also a line of code that ensures empty lines are not executed..

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Re: BGRAScript
« Reply #11 on: August 15, 2014, 08:44:02 am »
Great!

I have added a TSynCompletion, but nothing happens when I press Shift + Space (it just adds a space character in the TSynEdit); and there is no code highlighting. Are there some properties to set? Sorry, it's the first time I use SynEdit.
 
If you continue to improve that project, maybe it would be good to provide the project files.  :)
My projects are on Gitlab and on Codeberg.

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: BGRAScript
« Reply #12 on: August 15, 2014, 06:03:50 pm »
Great!

I have added a TSynCompletion, but nothing happens when I press Shift + Space (it just adds a space character in the TSynEdit); and there is no code highlighting. Are there some properties to set? Sorry, it's the first time I use SynEdit.
 
If you continue to improve that project, maybe it would be good to provide the project files.  :)

I will include this unit in BGRAControls package.

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: BGRAScript
« Reply #13 on: August 15, 2014, 08:20:18 pm »
That's great.

I would suggest to trim the string and retrieve the first parameter using the position of the first space with Pos, so that a command line would look like:
Rectangle 10,10,90,90,"rgba(100,100,100,100)","rgb(100,0,100,100)"

I've added this to BGRAControls. That's already possible, seems that CSV in TStrings accept space and comma to separate values.

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: BGRAScript
« Reply #14 on: August 15, 2014, 09:10:14 pm »
Congrats. Oh so it is possible to mix comma and space in the same CommaText?
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018