Files
CMLeonOS/Gui/UILib/Button.cs

185 lines
5.4 KiB
C#
Raw Normal View History

2026-03-08 20:22:53 +08:00
// The CMLeonOS Project (https://github.com/Leonmmcoset/CMLeonOS)
// Copyright (C) 2025-present LeonOS 2 Developer Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2026-03-01 17:03:49 +08:00
using Cosmos.System.Graphics;
2026-03-23 21:34:21 +08:00
using Cosmos.System;
2026-03-01 17:03:49 +08:00
using System;
using System.Drawing;
namespace CMLeonOS.Gui.UILib
{
internal class Button : Control
{
public Button(Window parent, int x, int y, int width, int height) : base(parent, x, y, width, height)
{
2026-03-23 21:34:21 +08:00
OnDown = (_, _) =>
{
held = true;
Render();
};
2026-03-01 17:03:49 +08:00
}
internal enum ButtonImageLocation
{
AboveText,
Left
}
private string _text = "Button";
internal string Text
{
get
{
return _text;
}
set
{
_text = value;
Render();
}
}
private ButtonImageLocation _imageLocation = ButtonImageLocation.AboveText;
internal ButtonImageLocation ImageLocation
{
get
{
return _imageLocation;
}
set
{
_imageLocation = value;
Render();
}
}
2026-03-23 21:34:21 +08:00
private Color _background = UITheme.Accent;
2026-03-01 17:03:49 +08:00
internal Color Background
{
get
{
return _background;
}
set
{
_background = value;
Render();
}
}
private Color _foreground = Color.White;
internal Color Foreground
{
get
{
return _foreground;
}
set
{
_foreground = value;
Render();
}
}
2026-03-23 21:34:21 +08:00
private Color _border = UITheme.AccentDark;
2026-03-01 17:03:49 +08:00
internal Color Border
{
get
{
return _border;
}
set
{
_border = value;
Render();
}
}
private Bitmap _image;
internal Bitmap Image
{
get
{
return _image;
}
set
{
_image = value;
Render();
}
}
2026-03-23 21:34:21 +08:00
private bool held = false;
2026-03-01 17:03:49 +08:00
internal override void Render()
{
2026-03-23 21:34:21 +08:00
if (held && MouseManager.MouseState != MouseState.Left)
{
held = false;
}
Color buttonBackground = held
? Color.FromArgb(Math.Max(0, Background.R - 24), Math.Max(0, Background.G - 24), Math.Max(0, Background.B - 24))
: Background;
Clear(UITheme.Surface);
DrawFilledRectangle(0, 0, Width, Height, buttonBackground);
DrawHorizontalLine(Width - 2, 1, 1, Color.FromArgb(
Math.Min(255, buttonBackground.R + 20),
Math.Min(255, buttonBackground.G + 20),
Math.Min(255, buttonBackground.B + 20)));
2026-03-01 17:03:49 +08:00
if (_image != null)
{
switch (_imageLocation)
{
case ButtonImageLocation.Left:
2026-03-23 21:34:21 +08:00
{
int imageWidth = (int)_image.Width;
int imageHeight = (int)_image.Height;
int textWidth = 8 * Text.Length;
int contentWidth = imageWidth + 6 + textWidth;
int imageX = Math.Max(4, (Width / 2) - (contentWidth / 2));
int imageY = (Height / 2) - (imageHeight / 2);
int textXLeft = imageX + imageWidth + 6;
int textYLeft = (Height / 2) - (16 / 2);
DrawImageAlpha(_image, imageX, imageY);
DrawString(Text, Foreground, textXLeft, textYLeft);
2026-03-01 17:03:49 +08:00
break;
2026-03-23 21:34:21 +08:00
}
2026-03-01 17:03:49 +08:00
case ButtonImageLocation.AboveText:
2026-03-23 21:34:21 +08:00
{
DrawImageAlpha(_image, (int)((Width / 2) - (_image.Width / 2)), Math.Max(1, (int)((Height / 2) - (_image.Height / 2) - 4)));
DrawString(Text, Foreground, (Width / 2) - (4 * Text.Length), Height - 17);
2026-03-01 17:03:49 +08:00
break;
2026-03-23 21:34:21 +08:00
}
2026-03-01 17:03:49 +08:00
default:
throw new Exception("Unrecognised image location in button.");
}
}
else
{
2026-03-23 21:34:21 +08:00
DrawString(Text, Foreground, (Width / 2) - (4 * Text.Length), (Height / 2) - 8 + (held ? 1 : 0));
2026-03-01 17:03:49 +08:00
}
DrawRectangle(0, 0, Width, Height, Border);
WM.Update(this);
}
}
}