added tooltip

This commit is contained in:
MasterGordon 2022-12-18 04:05:57 +01:00
parent fe5706e65f
commit 06d8c5bacf
13 changed files with 289 additions and 32 deletions

24
Mine2d/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net7.0/Mine2d.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

41
Mine2d/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Mine2d.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Mine2d.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Mine2d.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

View File

@ -92,12 +92,17 @@ public class Renderer
this.color = color.ToSdlColor();
}
public void DrawText(string text, int x, int y, bool center = false)
public (IntPtr texture, int width, int height, IntPtr surfaceMessage) CreateTextTexture(string text)
{
var surfaceMessage = TTF_RenderText_Solid(this.font, text, this.color);
var texture = SDL_CreateTextureFromSurface(this.renderer, surfaceMessage);
ProcessStatus(SDL_QueryTexture(texture, out _, out _, out var width, out var height));
return (texture, width, height, surfaceMessage);
}
public void DrawText(string text, int x, int y, bool center = false)
{
var (texture, width, height, surfaceMessage) = CreateTextTexture(text);
var rect = new SDL_Rect
{
@ -139,6 +144,24 @@ public class Renderer
};
ProcessStatus(SDL_RenderCopy(this.renderer, texture, IntPtr.Zero, ref rect));
}
public void DrawTexture(IntPtr texture, int x, int y, int w, int h, int srcX, int srcY, int srcWidth, int srcHeight)
{
SDL_Rect rect = new()
{
x = x,
y = y,
w = w,
h = h
};
SDL_Rect srcRect = new()
{
x = srcX,
y = srcY,
w = srcWidth,
h = srcHeight,
};
ProcessStatus(SDL_RenderCopy(this.renderer, texture, ref srcRect, ref rect));
}
public void DrawTexture(IntPtr texture, int x, int y, int w, int h, int offsetIndex, int srcWidth, int srcHeight)
{

View File

@ -35,8 +35,6 @@ public class PlayerEntity
{
var world = Context.Get().GameState.World;
bool hasCollision;
bool hasSetGround = false;
bool ground = false;
do
{
var pL = p.Position + new Vector2(0, -8);

View File

@ -12,11 +12,11 @@ public class PlayerBreakInput
public static void OnTick()
{
var ctx = Context.Get();
ctx.FrontendGameState.MousePosition = mousePos;
ctx.FrontendGameState.CursorPosition = mousePos;
if (ctx.GameState.Players.Find(player => player.Id == ctx.FrontendGameState.PlayerGuid)?.Mining
!= Vector2.Zero)
{
var amp = ctx.FrontendGameState.MousePosition
var amp = ctx.FrontendGameState.CursorPosition
/ ctx.FrontendGameState.Settings.GameScale
+ ctx.FrontendGameState.Camera.Position;
@ -45,7 +45,7 @@ public class PlayerBreakInput
}
var ctx = Context.Get();
var amp = ctx.FrontendGameState.MousePosition
var amp = ctx.FrontendGameState.CursorPosition
/ ctx.FrontendGameState.Settings.GameScale
+ ctx.FrontendGameState.Camera.Position;

View File

@ -16,7 +16,7 @@ public class PlayerPlaceInput
}
var ctx = Context.Get();
var amp = ctx.FrontendGameState.MousePosition
var amp = ctx.FrontendGameState.CursorPosition
/ ctx.FrontendGameState.Settings.GameScale
+ ctx.FrontendGameState.Camera.Position;

View File

@ -14,10 +14,12 @@ public class GameRenderer : IRenderer
this.renderers.Add(new ItemRenderer());
this.renderers.Add(new WorldCursorRenderer());
this.renderers.Add(new HudRenderer());
this.renderers.Add(new TooltipRenderer());
}
public void Render()
{
Context.Get().FrontendGameState.Tooltip = null;
foreach (var renderer in this.renderers)
{
renderer.Render();

View File

@ -28,6 +28,11 @@ public class HudRenderer : IRenderer
}
public void Render()
{
this.renderHotbar();
}
private void renderHotbar()
{
var renderer = Context.Get().Renderer;
var uiScale = Context.Get().FrontendGameState.Settings.UiScale;
@ -38,6 +43,7 @@ public class HudRenderer : IRenderer
var player = PlayerEntity.GetSelf();
renderer.DrawTexture(this.hotbarTexture, 0, 0, hotbarWidth * uiScale, hotbarHeight * uiScale);
renderer.DrawTexture(this.hotbarActiveTexture, activeSlot * 24 * uiScale, 0, 24 * uiScale, 24 * uiScale);
var cursorPosition = Context.Get().FrontendGameState.CursorPosition;
for (var i = 0; i < player?.Inventory.Hotbar.Length; i++)
{
var stack = player.Inventory.Hotbar[i];
@ -49,6 +55,10 @@ public class HudRenderer : IRenderer
var texture = stack.GetTexture();
renderer.DrawTexture(texture, (4 + i * 20) * uiScale, 4 * uiScale, 16 * uiScale, 16 * uiScale);
renderer.DrawText("" + stack.Count, (4 + i * 20) * uiScale, 20 * uiScale);
if(cursorPosition.X >= (4 + i * 20) * uiScale && cursorPosition.X <= (4 + i * 20 + 16) * uiScale && cursorPosition.Y >= 4 * uiScale && cursorPosition.Y <= (4 + 16) * uiScale)
{
Context.Get().FrontendGameState.Tooltip = stack.GetName();
}
}
}
}

View File

@ -0,0 +1,156 @@
using Mine2d.engine;
namespace Mine2d.game.frontend.renderer;
public class TooltipRenderer : IRenderer
{
private const int TooltipTextureWidth = 9;
private const int TooltipTextureHeight = 9;
private readonly IntPtr tooltipTexture;
public TooltipRenderer()
{
var rl = Context.Get().ResourceLoader;
var (ptr, size) = rl.LoadToIntPtr("assets.hud.tooltip.png");
var sdlBuffer = SDL_RWFromMem(ptr, size);
var surface = IMG_Load_RW(sdlBuffer, 1);
this.tooltipTexture = Context.Get().Renderer.CreateTextureFromSurface(surface);
SDL_FreeSurface(surface);
}
public void Render()
{
var ctx = Context.Get();
var tooltip = ctx.FrontendGameState.Tooltip;
if(tooltip == null || tooltip.Trim().Length == 0)
{
return;
}
var renderer = ctx.Renderer;
var uiScale = ctx.FrontendGameState.Settings.UiScale;
var tooltipPosition = ctx.FrontendGameState.CursorPosition + new Vector2(8, 8);
var tooltipX = (int)tooltipPosition.X;
var tooltipY = (int)tooltipPosition.Y;
var (texture, width, height, surfaceMessage) = renderer.CreateTextTexture(tooltip);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX,
tooltipY,
4 * uiScale,
4 * uiScale,
0,
0,
4,
4
);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX + 4 * uiScale,
tooltipY,
width,
4 * uiScale,
4,
0,
1,
4
);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX + width + 4 * uiScale,
tooltipY,
4 * uiScale,
4 * uiScale,
5,
0,
4,
4
);
Console.WriteLine(height);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX,
tooltipY + 4 * uiScale,
4 * uiScale,
height,
0,
4,
4,
1
);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX + 4 * uiScale,
tooltipY + 4 * uiScale,
width,
height,
4,
4,
1,
1
);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX + width + 4 * uiScale,
tooltipY + 4 * uiScale,
4 * uiScale,
height,
5,
4,
4,
1
);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX,
tooltipY + height + 4 * uiScale,
4 * uiScale,
4 * uiScale,
0,
5,
4,
4
);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX + 4 * uiScale,
tooltipY + height + 4 * uiScale,
width,
4 * uiScale,
4,
5,
1,
4
);
renderer.DrawTexture(
this.tooltipTexture,
tooltipX + width + 4 * uiScale,
tooltipY + height + 4 * uiScale,
4 * uiScale,
4 * uiScale,
5,
5,
4,
4
);
renderer.DrawTexture(
texture,
tooltipX + 4 * uiScale,
tooltipY + 4 * uiScale,
width,
height
);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surfaceMessage);
}
}

View File

@ -10,7 +10,7 @@ public class WorldCursorRenderer : IRenderer
var ctx = Context.Get();
var scale = ctx.FrontendGameState.Settings.GameScale;
var camera = ctx.FrontendGameState.Camera;
var absoluteMousePos = ctx.FrontendGameState.MousePosition / ctx.FrontendGameState.Settings.GameScale + camera.Position;
var absoluteMousePos = ctx.FrontendGameState.CursorPosition / ctx.FrontendGameState.Settings.GameScale + camera.Position;
if (PlayerEntity.GetSelf() == null || (absoluteMousePos - PlayerEntity.GetSelf().GetCenter()).LengthSquared() > Constants.BreakDistance * Constants.BreakDistance)
{
return;

View File

@ -0,0 +1,26 @@
using Mine2d.game.core;
namespace Mine2d.game.state;
public class FrontendGameState
{
public Vector2 MovementInput { get; set; }
public Vector2 CameraPosition { get; set; }
public int WindowWidth { get; set; }
public int WindowHeight { get; set; }
public Guid PlayerGuid { get; set; }
public Camera Camera { get; set; } = new();
public Vector2 CursorPosition { get; set; }
public Settings Settings { get; set; } = new Settings();
public string PlayerName { get; set; } = "Player";
public int HotbarIndex { get; set; }
public string Tooltip { get; set; } = "Test";
}
public class Settings
{
public int GameScale { get; set; } = 4;
public int UiScale { get; set; } = 3;
public bool ShowCollision { get; set; } = true;
public bool Fullscreen { get; set; } = false;
}

View File

@ -1,30 +1,7 @@
using Mine2d.game.core;
using Mine2d.game.core.data;
namespace Mine2d.game.state;
public class FrontendGameState
{
public Vector2 MovementInput { get; set; }
public Vector2 CameraPosition { get; set; }
public int WindowWidth { get; set; }
public int WindowHeight { get; set; }
public Guid PlayerGuid { get; set; }
public Camera Camera { get; set; } = new();
public Vector2 MousePosition { get; set; }
public Settings Settings { get; set; } = new Settings();
public string PlayerName { get; set; } = "Player";
public int HotbarIndex { get; set; }
}
public class Settings
{
public int GameScale { get; set; } = 6;
public int UiScale { get; set; } = 4;
public bool ShowCollision { get; set; } = true;
public bool Fullscreen { get; set; } = false;
}
public class GameState
{
public List<Player> Players { get; set; } = new List<Player>();