Launcher: Add a basic bitmap widget.

This commit is contained in:
UnknownShadow200 2016-10-06 12:35:02 +11:00
parent febe31b904
commit 01339e8172
5 changed files with 79 additions and 3 deletions

View File

@ -61,6 +61,9 @@ namespace Launcher.Gui.Screens {
game.ResetArea( label.X, label.Y, label.Width, label.Height );
RedrawWidget( label );
}
BitmapWidget bitmap = widget as BitmapWidget;
if( bitmap != null ) RedrawWidget( bitmap );
}
/// <summary>Redraws the given widget and marks the window as needing to be redrawn. </summary>

View File

@ -4,7 +4,7 @@ using System.Drawing;
using ClassicalSharp;
using Launcher.Gui.Widgets;
namespace Launcher.Gui.Views {
namespace Launcher.Gui.Views {
public sealed class DirectConnectView : IView {
internal int connectIndex, backIndex, ccSkinsIndex, statusIndex;
@ -45,7 +45,7 @@ namespace Launcher.Gui.Views {
connectIndex = widgetIndex;
Makers.Button( this, "Connect", 110, 35, titleFont )
.SetLocation( Anchor.Centre, Anchor.Centre, -110, 50 );
.SetLocation( Anchor.Centre, Anchor.Centre, -110, 50 );
backIndex = widgetIndex;
Makers.Button( this, "Back", 80, 35, titleFont )
.SetLocation( Anchor.Centre, Anchor.Centre, 125, 50 );

View File

@ -0,0 +1,58 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using ClassicalSharp;
namespace Launcher.Gui.Widgets {
/// <summary> Represents an image that cannot be modified by the user. </summary>
/// <remarks> Uses 4 bit (16 colour) palette. </remarks>
public sealed class BitmapWidget : Widget {
/// <summary> The raw indices within the palette that make up this image. </summary>
public byte[] Indices;
/// <summary> The ARGB palette for this image. </summary>
public FastColour[] Palette;
public BitmapWidget( LauncherWindow window, int size,
byte[] indices, FastColour[] palette ) : base( window ) {
Indices = indices;
Palette = palette;
Width = size; Height = size;
}
public unsafe override void Redraw( IDrawer2D drawer ) {
if( Window.Minimised || !Visible ) return;
int* palette = stackalloc int[Palette.Length];
CalculatePalette( palette );
using( FastBitmap bmp = Window.LockBits() ) {
int i = 0;
for( int yy = 0; yy < Height; yy++ ) {
if( (Y + yy) < 0 ) continue;
if( (Y + yy) >= bmp.Height ) break;
int* row = bmp.GetRowPtr( Y + yy );
for( int xx = 0; xx < Width; xx++ ) {
int index = Indices[i >> 1]; // each byte has even and odd 4bits
int selector = 4 * ((i + 1) & 1);
index = (index >> selector) & 0xF;
i++;
int col = palette[index];
if( col == 0 ) continue; // transparent pixel
if( (X + xx) < 0 || (X + xx) >= bmp.Width ) continue;
row[X + xx] = col;
}
}
}
}
unsafe void CalculatePalette( int* palette ) {
for( int i = 0; i < Palette.Length; i++ ) {
FastColour col = Palette[i];
if( !Active ) col = FastColour.Scale( col, 0.7f );
palette[i] = col.ToArgb();
}
}
}
}

View File

@ -3,7 +3,7 @@ using System.Drawing;
using ClassicalSharp;
using Launcher.Gui.Views;
namespace Launcher.Gui.Widgets {
namespace Launcher.Gui.Widgets {
/// <summary> Helper methods to construct widgets. </summary>
public static class Makers {
@ -85,5 +85,19 @@ namespace Launcher.Gui.Widgets {
view.widgetIndex++;
return widget;
}
public static Widget Bitmap( IView view, byte[] indices,
FastColour[] palette, int size ) {
BitmapWidget widget;
if( view.widgets[view.widgetIndex] != null ) {
widget = (BitmapWidget)view.widgets[view.widgetIndex];
} else {
widget = new BitmapWidget( view.game, size, indices, palette );
view.widgets[view.widgetIndex] = widget;
}
view.widgetIndex++;
return widget;
}
}
}

View File

@ -85,6 +85,7 @@
<Compile Include="Gui\Views\ResourcesView.cs" />
<Compile Include="Gui\Views\ServersView.cs" />
<Compile Include="Gui\Views\UpdatesView.cs" />
<Compile Include="Gui\Widgets\BitmapWidget.cs" />
<Compile Include="Gui\Widgets\CheckboxWidget.cs" />
<Compile Include="Gui\Widgets\InputText.cs" />
<Compile Include="Gui\Widgets\ButtonWidget.cs" />