瀏覽代碼

more script tools

johann 5 年之前
父節點
當前提交
af21b6a0f9
共有 4 個文件被更改,包括 292 次插入13 次删除
  1. 28 0
      lazpaint/lazpainttype.pas
  2. 127 6
      lazpaint/tools/utool.pas
  3. 122 1
      scripts/lazpaint/tools.py
  4. 15 6
      scripts/test_tools.py

+ 28 - 0
lazpaint/lazpainttype.pas

@@ -341,6 +341,8 @@ procedure SetWindowFullHeight(AForm: TForm; AHeight: integer);
 procedure SetWindowFullSize(AForm: TForm; AWidth,AHeight: integer);
 procedure SetWindowTopLeftCorner(AForm: TForm; X,Y: integer);
 function GetWindowTopLeftCorner(AForm: TForm): TPoint;
+function PascalToCSSCase(AIdentifier: string): string;
+function CSSToPascalCase(AIdentifier: string): string;
 
 implementation
 
@@ -524,6 +526,32 @@ begin
   result := Point(AForm.Left,AForm.Top);
 end;
 
+function PascalToCSSCase(AIdentifier: string): string;
+var
+  i: Integer;
+begin
+  result := AIdentifier;
+  for i := length(result) downto 1 do
+    if result[i] <> lowercase(result[i]) then
+    begin
+      result[i] := lowercase(result[i]);
+      if i > 1 then Insert('-', result, i);
+    end;
+end;
+
+function CSSToPascalCase(AIdentifier: string): string;
+var
+  i: Integer;
+begin
+  result := AIdentifier;
+  for i := length(result) downto 1 do
+  begin
+    if (i = 1) or (result[i-1] = '-') then
+      result[i] := upcase(result[i]) else
+    if result[i] = '-' then delete(result, i, 1);
+  end;
+end;
+
 { TImageEntry }
 
 class function TImageEntry.Empty: TImageEntry;

+ 127 - 6
lazpaint/tools/utool.pas

@@ -243,6 +243,10 @@ type
     function GetTextFontSize: single;
     function GetTextFontStyle: TFontStyles;
     function GetTextureOpacity: byte;
+    function ScriptGetAliasing(AVars: TVariableSet): TScriptResult;
+    function ScriptGetArrowEnd(AVars: TVariableSet): TScriptResult;
+    function ScriptGetArrowSize(AVars: TVariableSet): TScriptResult;
+    function ScriptGetArrowStart(AVars: TVariableSet): TScriptResult;
     function ScriptGetBackColor(AVars: TVariableSet): TScriptResult;
     function ScriptGetBrushCount(AVars: TVariableSet): TScriptResult;
     function ScriptGetBrushIndex(AVars: TVariableSet): TScriptResult;
@@ -253,6 +257,8 @@ type
     function ScriptGetFontSize(AVars: TVariableSet): TScriptResult;
     function ScriptGetFontStyle(AVars: TVariableSet): TScriptResult;
     function ScriptGetJoinStyle(AVars: TVariableSet): TScriptResult;
+    function ScriptGetLightPosition(AVars: TVariableSet): TScriptResult;
+    function ScriptGetLineCap(AVars: TVariableSet): TScriptResult;
     function ScriptGetPenColor(AVars: TVariableSet): TScriptResult;
     function ScriptGetPenStyle(AVars: TVariableSet): TScriptResult;
     function ScriptGetPenWidth(AVars: TVariableSet): TScriptResult;
@@ -262,6 +268,10 @@ type
     function ScriptGetTextOutline(AVars: TVariableSet): TScriptResult;
     function ScriptGetTextPhong(AVars: TVariableSet): TScriptResult;
     function ScriptGetTolerance(AVars: TVariableSet): TScriptResult;
+    function ScriptSetAliasing(AVars: TVariableSet): TScriptResult;
+    function ScriptSetArrowEnd(AVars: TVariableSet): TScriptResult;
+    function ScriptSetArrowSize(AVars: TVariableSet): TScriptResult;
+    function ScriptSetArrowStart(AVars: TVariableSet): TScriptResult;
     function ScriptSetBackColor(AVars: TVariableSet): TScriptResult;
     function ScriptSetBrushIndex(AVars: TVariableSet): TScriptResult;
     function ScriptSetBrushSpacing(AVars: TVariableSet): TScriptResult;
@@ -271,6 +281,8 @@ type
     function ScriptSetFontSize(AVars: TVariableSet): TScriptResult;
     function ScriptSetFontStyle(AVars: TVariableSet): TScriptResult;
     function ScriptSetJoinStyle(AVars: TVariableSet): TScriptResult;
+    function ScriptSetLightPosition(AVars: TVariableSet): TScriptResult;
+    function ScriptSetLineCap(AVars: TVariableSet): TScriptResult;
     function ScriptSetPenColor(AVars: TVariableSet): TScriptResult;
     function ScriptSetPenStyle(AVars: TVariableSet): TScriptResult;
     function ScriptSetPenWidth(AVars: TVariableSet): TScriptResult;
@@ -1377,6 +1389,30 @@ begin
   result := FTextureOpactiy;
 end;
 
+function TToolManager.ScriptGetAliasing(AVars: TVariableSet): TScriptResult;
+begin
+  AVars.Booleans['Result'] := toAliasing in ShapeOptions;
+  result := srOk;
+end;
+
+function TToolManager.ScriptGetArrowEnd(AVars: TVariableSet): TScriptResult;
+begin
+  AVars.Strings['Result'] := CSSToPascalCase(ArrowKindToStr[ArrowEnd]);
+  result := srOk;
+end;
+
+function TToolManager.ScriptGetArrowSize(AVars: TVariableSet): TScriptResult;
+begin
+  AVars.Points2D['Result'] := ArrowSize;
+  result := srOk;
+end;
+
+function TToolManager.ScriptGetArrowStart(AVars: TVariableSet): TScriptResult;
+begin
+  AVars.Strings['Result'] := CSSToPascalCase(ArrowKindToStr[ArrowStart]);
+  result := srOk;
+end;
+
 function TToolManager.ScriptGetBackColor(AVars: TVariableSet): TScriptResult;
 begin
   AVars.Pixels['Result'] := BackColor;
@@ -1457,6 +1493,23 @@ begin
   end;
 end;
 
+function TToolManager.ScriptGetLightPosition(AVars: TVariableSet): TScriptResult;
+begin
+  AVars.Points2D['Result'] := LightPosition;
+  result := srOk;
+end;
+
+function TToolManager.ScriptGetLineCap(AVars: TVariableSet): TScriptResult;
+begin
+  case LineCap of
+  pecSquare: AVars.Strings['Result'] := 'Square';
+  pecRound: AVars.Strings['Result'] := 'Round';
+  pecFlat: AVars.Strings['Result'] := 'Flat';
+  else exit(srException);
+  end;
+  result := srOk;
+end;
+
 function TToolManager.ScriptGetPenColor(AVars: TVariableSet): TScriptResult;
 begin
   AVars.Pixels['Result'] := ForeColor;
@@ -1491,7 +1544,6 @@ begin
   for opt := low(TShapeOption) to high(TShapeOption) do
     if opt in ShapeOptions then
     case opt of
-    toAliasing: AVars.AppendString(options, 'Aliasing');
     toDrawShape: Avars.AppendString(options, 'DrawShape');
     toFillShape: Avars.AppendString(options, 'FillShape');
     toCloseShape: Avars.AppendString(options, 'CloseShape');
@@ -1537,6 +1589,49 @@ begin
   result := srOk;
 end;
 
+function TToolManager.ScriptSetAliasing(AVars: TVariableSet): TScriptResult;
+begin
+  if AVars.Booleans['Enabled'] then
+    ShapeOptions:= ShapeOptions + [toAliasing]
+  else
+    ShapeOptions:= ShapeOptions - [toAliasing];
+  result := srOk;
+end;
+
+function TToolManager.ScriptSetArrowEnd(AVars: TVariableSet): TScriptResult;
+var ak: TArrowKind;
+  kindStr: String;
+begin
+  kindStr := PascalToCSSCase(AVars.Strings['Kind']);
+  ak := StrToArrowKind(kindStr);
+  if (ak = akNone) and (kindStr <> ArrowKindToStr[akNone]) then
+    exit(srInvalidParameters);
+  ArrowEnd := ak;
+  result := srOk;
+end;
+
+function TToolManager.ScriptSetArrowSize(AVars: TVariableSet): TScriptResult;
+var
+  s: TPointF;
+begin
+  s := AVars.Points2D['Size'];
+  if isEmptyPointF(s) then exit(srInvalidParameters);
+  ArrowSize := s;
+  result := srOk;
+end;
+
+function TToolManager.ScriptSetArrowStart(AVars: TVariableSet): TScriptResult;
+var ak: TArrowKind;
+  kindStr: String;
+begin
+  kindStr := PascalToCSSCase(AVars.Strings['Kind']);
+  ak := StrToArrowKind(kindStr);
+  if (ak = akNone) and (kindStr <> ArrowKindToStr[akNone]) then
+    exit(srInvalidParameters);
+  ArrowStart := ak;
+  result := srOk;
+end;
+
 function TToolManager.ScriptSetBackColor(AVars: TVariableSet): TScriptResult;
 begin
   BackColor := AVars.Pixels['Color'];
@@ -1625,6 +1720,30 @@ begin
   end;
 end;
 
+function TToolManager.ScriptSetLightPosition(AVars: TVariableSet): TScriptResult;
+var
+  ptF: TPointF;
+begin
+  ptF := AVars.Points2D['Position'];
+  if IsEmptyPointF(ptF) then exit(srInvalidParameters);
+  LightPosition := ptF;
+  result := srOk;
+end;
+
+function TToolManager.ScriptSetLineCap(AVars: TVariableSet): TScriptResult;
+var
+  capStr: String;
+begin
+  capStr := AVars.Strings['Cap'];
+  case capStr of
+  'Round': LineCap := pecRound;
+  'Square': LineCap := pecSquare;
+  'Flat': LineCap := pecFlat;
+  else exit(srInvalidParameters);
+  end;
+  result := srOk;
+end;
+
 function TToolManager.ScriptSetPenColor(AVars: TVariableSet): TScriptResult;
 begin
   ForeColor := AVars.Pixels['Color'];
@@ -1658,12 +1777,12 @@ var so: TShapeOptions;
   opt: String;
 begin
   so := [];
+  if toAliasing in ShapeOptions then include(so, toAliasing);
   options := AVars.GetVariable('Options');
   for i := 0 to AVars.GetListCount(options)-1 do
   begin
     opt := AVars.GetStringAt(options, i);
     case opt of
-    'Aliasing': include(so, toAliasing);
     'DrawShape': include(so, toDrawShape);
     'FillShape': include(so, toFillShape);
     'CloseShape': include(so, toCloseShape);
@@ -2122,6 +2241,8 @@ begin
   FScriptContext.RegisterScriptFunction('ToolGetJoinStyle', @ScriptGetJoinStyle, ARegister);
   FScriptContext.RegisterScriptFunction('ToolSetShapeOptions', @ScriptSetShapeOptions, ARegister);
   FScriptContext.RegisterScriptFunction('ToolGetShapeOptions', @ScriptGetShapeOptions, ARegister);
+  FScriptContext.RegisterScriptFunction('ToolSetAliasing', @ScriptSetAliasing, ARegister);
+  FScriptContext.RegisterScriptFunction('ToolGetAliasing', @ScriptGetAliasing, ARegister);
   FScriptContext.RegisterScriptFunction('ToolSetShapeRatio', @ScriptSetShapeRatio, ARegister);
   FScriptContext.RegisterScriptFunction('ToolGetShapeRatio', @ScriptGetShapeRatio, ARegister);
   FScriptContext.RegisterScriptFunction('ToolSetBrushIndex', @ScriptSetBrushIndex, ARegister);
@@ -2141,10 +2262,10 @@ begin
   FScriptContext.RegisterScriptFunction('ToolGetTextOutline', @ScriptGetTextOutline, ARegister);
   FScriptContext.RegisterScriptFunction('ToolSetTextPhong', @ScriptSetTextPhong, ARegister);
   FScriptContext.RegisterScriptFunction('ToolGetTextPhong', @ScriptGetTextPhong, ARegister);
-{  FScriptContext.RegisterScriptFunction('ToolSetLightPosition', @ScriptSetLightPosition, ARegister);
+  FScriptContext.RegisterScriptFunction('ToolSetLightPosition', @ScriptSetLightPosition, ARegister);
   FScriptContext.RegisterScriptFunction('ToolGetLightPosition', @ScriptGetLightPosition, ARegister);
-  FScriptContext.RegisterScriptFunction('ToolSetLightAltitude', @ScriptSetLightAltitude, ARegister);
-  FScriptContext.RegisterScriptFunction('ToolGetLightAltitude', @ScriptGetLightAltitude, ARegister);
+{  FScriptContext.RegisterScriptFunction('ToolSetLightAltitude', @ScriptSetLightAltitude, ARegister);
+  FScriptContext.RegisterScriptFunction('ToolGetLightAltitude', @ScriptGetLightAltitude, ARegister);}
   FScriptContext.RegisterScriptFunction('ToolSetLineCap', @ScriptSetLineCap, ARegister);
   FScriptContext.RegisterScriptFunction('ToolGetLineCap', @ScriptGetLineCap, ARegister);
   FScriptContext.RegisterScriptFunction('ToolSetArrowStart', @ScriptSetArrowStart, ARegister);
@@ -2153,7 +2274,7 @@ begin
   FScriptContext.RegisterScriptFunction('ToolGetArrowEnd', @ScriptGetArrowEnd, ARegister);
   FScriptContext.RegisterScriptFunction('ToolSetArrowSize', @ScriptSetArrowSize, ARegister);
   FScriptContext.RegisterScriptFunction('ToolGetArrowSize', @ScriptGetArrowSize, ARegister);
-  FScriptContext.RegisterScriptFunction('ToolSetSplineStyle', @ScriptSetSplineStyle, ARegister);
+{  FScriptContext.RegisterScriptFunction('ToolSetSplineStyle', @ScriptSetSplineStyle, ARegister);
   FScriptContext.RegisterScriptFunction('ToolGetSplineStyle', @ScriptGetSplineStyle, ARegister);
   FScriptContext.RegisterScriptFunction('ToolSetGradientType', @ScriptSetGradientType, ARegister);
   FScriptContext.RegisterScriptFunction('ToolGetGradientType', @ScriptGetGradientType, ARegister);

+ 122 - 1
scripts/lazpaint/tools.py

@@ -51,11 +51,37 @@ JOIN_STYLE_BEVEL = 'Bevel'
 JOIN_STYLE_MITER = 'Miter'
 JOIN_STYLE_ROUND = 'Round'
 
-SHAPE_OPTION_ALIASING = 'Aliasing'
+LINE_CAP_ROUND = 'Round'
+LINE_CAP_SQUARE = 'Square'
+LINE_CAP_FLAT = 'Flat'
+
+FONT_STYLE_BOLD = 'Bold'
+FONT_STYLE_ITALIC = 'Italic'
+FONT_STYLE_UNDERLINE = 'Underline'
+FONT_STYLE_STRIKE_OUT = 'StrikeOut'
+
+ALIGN_LEFT = 'Left'
+ALIGN_CENTER = 'Center'
+ALIGN_RIGHT = 'Right'
+
 SHAPE_OPTION_DRAW_SHAPE = 'DrawShape'
 SHAPE_OPTION_FILL_SHAPE = 'FillShape'
 SHAPE_OPTION_CLOSE_SHAPE = 'CloseShape'
 
+ARROW_NONE = 'None'
+ARROW_TAIL = 'Tail'
+ARROW_TIP = 'Tip'
+ARROW_NORMAL = 'Normal'
+ARROW_CUT = 'Cut'
+ARROW_FLIPPED = 'Flipped'
+ARROW_FLIPPED_CUT = 'FlippedCut'
+ARROW_TRIANGLE = 'Triangle'
+ARROW_TRIANGLE_BACK1 = 'TriangleBack1'
+ARROW_TRIANGLE_BACK2 = 'TriangleBack2'
+ARROW_HOLLOW_TRIANGLE = 'HollowTriangle'
+ARROW_HOLLOW_TRIANGLE_BACK1 = 'HollowTriangleBack1'
+ARROW_HOLLOW_TRIANGLE_BACK2 = 'HollowTriangleBack2'
+
 KEY_UNKNOWN = 'Unknown'
 KEY_BACKSPACE = 'Backspace'
 KEY_TAB = 'Tab'
@@ -198,9 +224,104 @@ def set_join_style(style):
 def get_join_style():
   return command.send('ToolGetJoinStyle?')
 
+def set_line_cap(cap):
+  command.send('ToolSetLineCap', Cap=cap)
+
+def get_line_cap():
+  return command.send('ToolGetLineCap?')
+
 def set_shape_options(options):
   command.send('ToolSetShapeOptions', Options=options)
 
 def get_shape_options():
   return command.send('ToolGetShapeOptions?')
 
+def set_aliasing(enabled):
+  command.send('ToolSetAliasing', Enabled=enabled)
+
+def get_aliasing():
+  return command.send('ToolGetAliasing?')
+
+def set_shape_ratio(ratio):
+  command.send('ToolSetShapeRatio', Ratio=ratio)
+
+def get_shape_ratio():
+  return command.send('ToolGetShapeRatio?')
+
+def set_brush_index(index):
+  command.send('ToolSetBrushIndex', Index=index)
+
+def get_brush_index():
+  return command.send('ToolGetBrushIndex?')
+
+def get_brush_count(): 
+  return command.send('ToolGetBrushCount?')
+
+def set_brush_spacing(index):
+  command.send('ToolSetBrushSpacing', Index=index)
+
+def get_brush_spacing():
+  return command.send('ToolGetBrushSpacing?')
+
+def set_font_name(name):
+  command.send('ToolSetFontName', Name=name)
+
+def get_font_name():
+  return command.send('ToolGetFontName?')
+
+def set_font_size(size):
+  command.send('ToolSetFontSize', Size=size)
+
+def get_font_size():
+  return command.send('ToolGetFontSize?')
+
+def set_font_style(style):
+  if not isinstance(style, list):
+    style = [style]
+  command.send('ToolSetFontStyle', Style=style)
+
+def get_font_style():
+  return command.send('ToolGetFontStyle?')
+
+def set_text_align(align):
+  command.send('ToolSetTextAlign', Align=align)
+
+def get_text_align():
+  return command.send('ToolGetTextAlign?')
+
+def set_text_outline(width):
+  command.send('ToolSetTextOutline', Width=width)
+
+def get_text_outline():
+  return command.send('ToolGetTextOutline?')
+
+def set_text_phong(enabled):
+  command.send('ToolSetTextPhong', Enabled=enabled)
+
+def get_text_phong():
+  return command.send('ToolGetTextPhong?')
+
+def set_light_position(x, y):
+  command.send('ToolSetLightPosition', Position=(x, y))
+
+def get_light_position():
+  return command.send('ToolGetLightPosition?')
+
+def set_arrow_start(arrow):
+  command.send('ToolSetArrowStart', Arrow=arrow)
+
+def get_arrow_start():
+  return command.send('ToolGetArrowStart?')
+
+def set_arrow_end(arrow):
+  command.send('ToolSetArrowEnd', Arrow=arrow)
+
+def get_arrow_end():
+  return command.send('ToolGetArrowEnd?')
+
+def set_arrow_size(x, y):
+  command.send('ToolSetArrowSize', Size=(x, y))
+
+def get_arrow_size():
+  return command.send('ToolGetArrowSize?')
+

+ 15 - 6
scripts/test_tools.py

@@ -3,6 +3,7 @@ from lazpaint import tools, image, layer, colors
 image.new(800, 600)
 
 tools.choose(tools.PEN)
+tools.set_pen_width(10)
 tools.set_pen_color(colors.RED)
 tools.set_back_color(colors.ORANGE)
 tools.mouse( (50,50) )
@@ -14,15 +15,23 @@ tools.set_pen_color(colors.YELLOW)
 tools.set_back_color(colors.BLUE)
 tools.keys(tools.KEY_RETURN)
 
+tools.choose(tools.PHONG_SHAPE)
+tools.set_light_position(500, 500)
+tools.mouse( [(50,300), (300,450)] )
+tools.set_light_position(50, 600)
+tools.keys(tools.KEY_RETURN)
+
 tools.choose(tools.TEXT)
 tools.mouse( [(50,150), (450,350)] )
 tools.set_pen_color(colors.BLACK)
+tools.set_font_style(tools.FONT_STYLE_ITALIC)
+tools.set_font_size(20)
+tools.set_text_phong(False)
 tools.write("Hello\nworld")
 
-layer.new()
-
-tools.choose(tools.TEXTURE_MAPPING)
-tools.mouse( [(300,50), (450,200)], [tools.STATE_LEFT, tools.STATE_SHIFT] )
-tools.mouse( [(300,50), (350,80)] )
-tools.keys(tools.KEY_RETURN)
+#layer.new()
+#tools.choose(tools.TEXTURE_MAPPING)
+#tools.mouse( [(300,50), (450,200)], [tools.STATE_LEFT, tools.STATE_SHIFT] )
+#tools.mouse( [(300,50), (350,80)] )
+#tools.keys(tools.KEY_RETURN)