Color Picker

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Post Reply
User avatar
Ats
Posts: 224
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Color Picker

Post by Ats » Sun Jul 22, 2018 12:15 pm

I wrote a little color picker :wink:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" MouseVisible="255" RenderOrder="1" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <BeforeInitExp>
<![CDATA[//
if(ANDROID) this.ModuleName = "libGLESv1_CM.so";
else this.ModuleName = "opengl32";]]>
      </BeforeInitExp>
      <Source>
<![CDATA[//
void glReadPixels(int x, int y, int width, int height, int format, int type, xptr data){}
//]]>
      </Source>
    </ZExternalLibrary>
    <SpawnModel Model="BackgroundModel"/>
    <SpawnModel Model="CubeModel"/>
  </OnLoaded>
  <Content>
    <Group Comment="Cube">
      <Children>
        <Material Name="CubeMaterial" Shading="1" Color="0 0 0 1"/>
        <Model Name="CubeModel">
          <OnSpawn>
            <ZExpression>
              <Expression>
<![CDATA[//
CurrentModel.RotationVelocity = .1;]]>
              </Expression>
            </ZExpression>
          </OnSpawn>
          <OnUpdate>
            <ZExpression>
              <Expression>
<![CDATA[//
int x = (1+App.MousePosition.X)*App.ScreenWidth*0.5;
int y = (1+App.MousePosition.Y)*App.ScreenHeight*0.5;
glReadPixels(x, y, 1, 1, 0x80E0, 0x1401, ColorPickerArray);
CubeMaterial.Color.R = ColorPickerArray[2]/256.0;
CubeMaterial.Color.G = ColorPickerArray[1]/256.0;
CubeMaterial.Color.B = ColorPickerArray[0]/256.0;]]>
              </Expression>
            </ZExpression>
          </OnUpdate>
          <OnRender>
            <UseMaterial Material="CubeMaterial"/>
            <RenderMesh Mesh="CubeMesh"/>
          </OnRender>
        </Model>
        <Mesh Name="CubeMesh">
          <Producers>
            <MeshBox/>
          </Producers>
        </Mesh>
      </Children>
    </Group>
    <Group Comment="Background">
      <Children>
        <Model Name="BackgroundModel">
          <OnRender>
            <UseMaterial Material="ColorsMaterial"/>
            <RenderMesh Mesh="BackgroundMesh"/>
          </OnRender>
        </Model>
        <Mesh Name="BackgroundMesh">
          <Producers>
            <MeshBox Scale="8 4.5 1" Grid2DOnly="255"/>
          </Producers>
        </Mesh>
        <Bitmap Name="ColorsBitmap" Width="128" Height="128">
          <Producers>
            <BitmapCells PointsPlacement="1" UsedMetrics="2" RandomSeed="3" BorderPixels="0"/>
          </Producers>
        </Bitmap>
        <Material Name="ColorsMaterial" Shading="1" Light="0" ZBuffer="0">
          <Textures>
            <MaterialTexture Texture="ColorsBitmap" TextureScale="2 2 1" TextureWrapMode="1" TexCoords="1"/>
          </Textures>
        </Material>
      </Children>
    </Group>
    <Array Name="ColorPickerArray" Type="4" SizeDim1="3"/>
  </Content>
</ZApplication>
By the way, is it possible to vary the BitmapCells RandomSeed over time?

User avatar
Kjell
Posts: 1698
Joined: Sat Feb 23, 2008 11:15 pm

Re: Color Picker

Post by Kjell » Sun Jul 22, 2018 12:52 pm

Hi Ats,
Ats wrote:By the way, is it possible to vary the BitmapCells RandomSeed over time?
You mean something like this?

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" MouseVisible="255" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <BeforeInitExp>
<![CDATA[//
if(ANDROID) this.ModuleName = "libGLESv1_CM.so";
else this.ModuleName = "opengl32";]]>
      </BeforeInitExp>
      <Source>
<![CDATA[//
void glReadPixels(int x, int y, int width, int height, int format, int type, xptr data){}
//]]>
      </Source>
    </ZExternalLibrary>
    <SpawnModel Model="BackgroundModel"/>
    <SpawnModel Model="CubeModel"/>
  </OnLoaded>
  <OnUpdate>
    <Timer Interval="1">
      <OnTimer>
        <ZExpression>
          <Expression>
<![CDATA[//

ColorsCells.RandomSeed = App.Time;
@RefreshContent(Component: ColorsBitmap);]]>
          </Expression>
        </ZExpression>
      </OnTimer>
    </Timer>
  </OnUpdate>
  <Content>
    <Group Comment="Cube">
      <Children>
        <Material Name="CubeMaterial" Shading="1" Color="0.4336 0.6836 0.0117 1"/>
        <Model Name="CubeModel">
          <OnSpawn>
            <ZExpression>
              <Expression>
<![CDATA[//
CurrentModel.RotationVelocity = .1;]]>
              </Expression>
            </ZExpression>
          </OnSpawn>
          <OnUpdate>
            <ZExpression>
              <Expression>
<![CDATA[//
int x = (1+App.MousePosition.X)*App.ScreenWidth*0.5;
int y = (1+App.MousePosition.Y)*App.ScreenHeight*0.5;
glReadPixels(x, y, 1, 1, 0x80E0, 0x1401, ColorPickerArray);
CubeMaterial.Color.R = ColorPickerArray[2]/256.0;
CubeMaterial.Color.G = ColorPickerArray[1]/256.0;
CubeMaterial.Color.B = ColorPickerArray[0]/256.0;]]>
              </Expression>
            </ZExpression>
          </OnUpdate>
          <OnRender>
            <UseMaterial Material="CubeMaterial"/>
            <RenderMesh Mesh="CubeMesh"/>
          </OnRender>
        </Model>
        <Mesh Name="CubeMesh">
          <Producers>
            <MeshBox/>
          </Producers>
        </Mesh>
      </Children>
    </Group>
    <Group Comment="Background">
      <Children>
        <Model Name="BackgroundModel">
          <OnRender>
            <UseMaterial Material="ColorsMaterial"/>
            <RenderMesh Mesh="BackgroundMesh"/>
          </OnRender>
        </Model>
        <Mesh Name="BackgroundMesh">
          <Producers>
            <MeshBox Scale="8 4.5 1" Grid2DOnly="255"/>
          </Producers>
        </Mesh>
        <Bitmap Name="ColorsBitmap" Width="128" Height="128">
          <Producers>
            <BitmapCells Name="ColorsCells" PointsPlacement="1" UsedMetrics="2" RandomSeed="12" BorderPixels="0"/>
          </Producers>
        </Bitmap>
        <Material Name="ColorsMaterial" Shading="1" Light="0" ZBuffer="0">
          <Textures>
            <MaterialTexture Texture="ColorsBitmap" TextureScale="2 2 1" TextureWrapMode="1" TexCoords="1"/>
          </Textures>
        </Material>
      </Children>
    </Group>
    <Array Name="ColorPickerArray" Type="4" SizeDim1="3"/>
  </Content>
</ZApplication>
K

User avatar
Ats
Posts: 224
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Color Picker

Post by Ats » Sun Jul 22, 2018 3:14 pm

Oh, I was missing RefreshContent!
Thanks Kjell

User avatar
Ats
Posts: 224
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Color Picker

Post by Ats » Thu Nov 15, 2018 9:08 am

I'm trying to use the color picker only on the background, not on the models that appear over it.
In the above example, the mouse cursor should give the background color under it, but not the color of the rotating cube that appears over it. I tried a lot of things without success. Is that possible?

Post Reply