Page 1 of 1

Altering "my background paint"

Posted: Tue Aug 20, 2019 10:43 pm
by pink
I am trying to change "my background paint" from within my widget. Actually I am trying to change all of the built-in colors/paints because I am trying to create color themes that can be saved and loaded. None of my attempts thus far have worked...

Here's a brief rundown of what I've tried, I can use any advice about what I've missed.

set the red of the color of my background paint to 0
set the green of the color of my background paint to 1
set the blue of the color of my background paint to 0

put solid paint with color [0,1,0] into tPaint
put tPaint into my background paint

put solid paint with color [0,1,0] into tPaint
set my background paint to tPaint

put color [0,1,0] into tColor
set the color of my background paint to tColor

put color [0,1,0] into tColor
set my background paint to solid paint with tColor


Here's my experimental widget:

Code: Select all

widget pink.mad.colorexperiment

metadata title is "MPTemplate"
metadata author is "MadPink"
metadata version is "0.0.0"
metadata preferredSize     is "23,42"
metadata svgicon is ""

use com.livecode.canvas
use com.livecode.widget
use com.livecode.engine
use com.livecode.foreign
use com.livecode.mathfoundation
use com.livecode.library.widgetutils

--PROPERTIES
metadata foregroundColor.editor        is "com.livecode.pi.color"
metadata foregroundColor.default is "255,255,255"
metadata foregroundColor.section is "Colors"
metadata foregroundColor.label is "Text Color"

metadata backgroundColor.editor        is "com.livecode.pi.color"
metadata backgroundColor.default is "255,15,192"
metadata backgroundColor.section is "Colors"
metadata backgroundColor.label is "Background Color"

metadata textFont.editor is "com.livecode.pi.font"
metadata textFont.default     is "Arial"

metadata textSize.editor is "com.livecode.pi.number"
metadata textSize.default     is "15"

property someText        get mText           set setText
metadata someText.editor      is "com.livecode.pi.string"
metadata someText.default     is "Hello World"
metadata someText.section         is "Basic"
private variable mText as String

property makeGreen  get mMakeGreen  set setMakeGreen
metadata makeGreen.editor  is "com.livecode.pi.boolean"
metadata makeGreen.default  is "false"
metadata makeGreen.label  is "Make the background green"
metadata makeGreen.section  is "Advanced"
private variable mMakeGreen as Boolean

private handler setMakeGreen(in pVal as Boolean)
   variable tPaint as Paint
   variable tCOlor as Color
  put pVal into mMakeGreen
  if pVal is true then
     -- set the red of the color of my background paint to 0
     -- set the green of the color of my background paint to 1
     -- set the blue of the color of my background paint to 0

     -- put solid paint with color [0,1,0] into tPaint
     -- put tPaint into my background paint
     -- set my background paint to tPaint

     put color [0,1,0] into tColor
     -- set the color of my background paint to tColor
     -- set my background paint to solid paint with tColor
    redraw all
  end if
end handler

--HANDLERS to set properties
private handler setText(in pText as String)
    put pText into mText
end handler

--SCRIPT VARIABLES
private variable mStuff as String

--HANDLERS
public handler OnCreate()
    put "Hello World" into mText
end handler

public handler OnSave(out rProperties as Array)
    put the empty array into rProperties
    put mText into rProperties["someText"]
end handler

public handler OnLoad(in pProperties as Array)
    put pProperties["someText"] into mText
end handler

public handler OnPaint()
   variable tRect as Rectangle

   set the paint of this canvas to my background paint
   put rectangle [0,0,my width,my height] into tRect
   fill rounded rectangle path of tRect with radius 23 on this canvas
end handler

public handler userAction()
  --some sort of custom interaction
  --put some data into mAppData that can be later read in the script
  post "userAction"
end handler



end widget

Re: Altering "my background paint"

Posted: Wed Aug 21, 2019 12:28 am
by [-hh]
This is how I understand it: "my background paint" is read only.
It is the "effective" result of your current paint settings (pattern or color) for the canvas.

Similarly you can't set my bounds, my children, my disabled, my enabled, my font, my height, my name, my native layer, my pixel scale, my rectangle, my resources folder, my script object, my width.

Probably this is meant to have "set the background paint of this canvas to tPaint".

But I don't think the property "background paint" etc. of a canvas is already implemented. At least I used after unsuccessful tests a while ago until now only "set the paint of this canvas to mPaint". Didn't test in LC 9.5.

Re: Altering "my background paint"

Posted: Wed Aug 21, 2019 12:52 am
by bn
here are some modifications that make "makeGreen" functional and also displays the text.

Code: Select all

widget pink.mad.colorexperiment

metadata title is "MPTemplate"
metadata author is "MadPink"
metadata version is "0.0.0"
metadata preferredSize     is "23,42"
metadata svgicon is ""

use com.livecode.canvas
use com.livecode.widget
use com.livecode.engine
use com.livecode.foreign
use com.livecode.mathfoundation
use com.livecode.library.widgetutils

--PROPERTIES
metadata foregroundColor.editor        is "com.livecode.pi.color"
metadata foregroundColor.default is "255,255,255"
metadata foregroundColor.section is "Colors"
metadata foregroundColor.label is "Text Color"

metadata backgroundColor.editor        is "com.livecode.pi.color"
metadata backgroundColor.default is "255,15,192"
metadata backgroundColor.section is "Colors"
metadata backgroundColor.label is "Background Color"

metadata textFont.editor is "com.livecode.pi.font"
metadata textFont.default     is "Arial"

metadata textSize.editor is "com.livecode.pi.number"
metadata textSize.default     is "15"

property someText        get mText           set setText
metadata someText.editor      is "com.livecode.pi.string"
metadata someText.default     is "Hello World"
metadata someText.section         is "Basic"
private variable mText as String

property makeGreen  get mMakeGreen  set setMakeGreen
metadata makeGreen.editor  is "com.livecode.pi.boolean"
metadata makeGreen.default  is "false"
metadata makeGreen.label  is "Make the background green"
metadata makeGreen.section  is "Advanced"
private variable mMakeGreen as Boolean
private variable mGreen as Color

private handler setMakeGreen(in pVal as Boolean)

  put pVal into mMakeGreen
  if pVal is true then

     put color [0,1,0] into mGreen

  end if
  redraw all
end handler

--HANDLERS to set properties
private handler setText(in pText as String)
    put pText into mText
    redraw all
end handler

--SCRIPT VARIABLES
private variable mStuff as String

--HANDLERS
public handler OnCreate()
    put "Hello World" into mText
    put color [0,1,0] into mGreen
end handler

public handler OnSave(out rProperties as Array)
    put the empty array into rProperties
    put mText into rProperties["someText"]
    put mGreen into rProperties["mGreen"]
end handler

public handler OnLoad(in pProperties as Array)
    put pProperties["someText"] into mText
    put pProperties ["mGreen"] into mGreen
end handler

public handler OnPaint()
   variable tRect as Rectangle
   if mMakeGreen then
      set the paint of this canvas to solid paint with mGreen
   else
      set the paint of this canvas to my background paint
   end if
   put rectangle [0,0,my width,my height] into tRect
   fill rounded rectangle path of tRect with radius 23 on this canvas

   /* draw text */
   variable tBounds as Rectangle
   measure mText on this canvas
   put the result into tBounds
   set the left of tBounds to the left of my bounds
   set the top of tBounds to my height/2
   set the paint of this canvas to my foreground paint
   fill text mText at top left of tBounds on this canvas
end handler

public handler userAction()
  --some sort of custom interaction
  --put some data into mAppData that can be later read in the script
  post "userAction"
end handler

end widget
Kind regards
Bernd

Re: Altering "my background paint"

Posted: Wed Aug 21, 2019 2:47 am
by pink
Hi Bernd,

That's not what I'm trying to do. I already use background/foreground/shadow/top/bottom paints etc... I want to be able to alter them within the widget itself, I also still want to be set them as their current existing properties.

Re: Altering "my background paint"

Posted: Wed Aug 21, 2019 3:45 am
by bn
I want to be able to alter them within the widget itself, I also still want to be set them as their current existing properties.
See Hermann's response.
The example I gave is an effective override of the background paint.
Kind regards
Bernd

Re: Altering "my background paint"

Posted: Wed Aug 21, 2019 12:27 pm
by pink
I missed Hermann's response...

Well, this means I have a lot of retooling to do... :?