Color Picker

Share your ZGE-development tips and techniques here!

Moderator: Moderators

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

Color Picker

Postby 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: 1644
Joined: Sat Feb 23, 2008 11:15 pm

Re: Color Picker

Postby 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: 127
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Color Picker

Postby Ats » Sun Jul 22, 2018 3:14 pm

Oh, I was missing RefreshContent!
Thanks Kjell


Return to “Tips'n'Tricks”

Who is online

Users browsing this forum: Bing [Bot] and 0 guests