Refactor some computer screen logic
- Move some shared Gui{Computer,Turtle} code into a new class. Using entirely different naming conventions because of course (they are consistent with MojMap, just not the rest of CC:T). - Fix some mouse scaling issues in the terminal.
This commit is contained in:
parent
2227845658
commit
2b029bd506
|
@ -0,0 +1,94 @@
|
||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
package dan200.computercraft.client.gui;
|
||||||
|
|
||||||
|
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
|
||||||
|
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
||||||
|
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||||
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
|
import dan200.computercraft.shared.computer.inventory.ContainerComputerBase;
|
||||||
|
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
|
||||||
|
import net.minecraft.client.gui.widget.Widget;
|
||||||
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
|
import net.minecraft.util.text.ITextComponent;
|
||||||
|
import org.lwjgl.glfw.GLFW;
|
||||||
|
|
||||||
|
public abstract class ComputerScreenBase<T extends ContainerComputerBase> extends ContainerScreen<T>
|
||||||
|
{
|
||||||
|
protected WidgetTerminal terminal;
|
||||||
|
protected final ClientComputer computer;
|
||||||
|
protected final ComputerFamily family;
|
||||||
|
|
||||||
|
protected final int sidebarYOffset;
|
||||||
|
|
||||||
|
public ComputerScreenBase( T container, PlayerInventory player, ITextComponent title, int sidebarYOffset )
|
||||||
|
{
|
||||||
|
super( container, player, title );
|
||||||
|
computer = (ClientComputer) container.getComputer();
|
||||||
|
family = container.getFamily();
|
||||||
|
this.sidebarYOffset = sidebarYOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract WidgetTerminal createTerminal();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected final void init()
|
||||||
|
{
|
||||||
|
super.init();
|
||||||
|
minecraft.keyboardHandler.setSendRepeatsToGui( true );
|
||||||
|
|
||||||
|
terminal = addButton( createTerminal() );
|
||||||
|
ComputerSidebar.addButtons( this, computer, this::addButton, leftPos, topPos + sidebarYOffset );
|
||||||
|
setFocused( terminal );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void removed()
|
||||||
|
{
|
||||||
|
super.removed();
|
||||||
|
minecraft.keyboardHandler.setSendRepeatsToGui( false );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void tick()
|
||||||
|
{
|
||||||
|
super.tick();
|
||||||
|
terminal.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final boolean keyPressed( int key, int scancode, int modifiers )
|
||||||
|
{
|
||||||
|
// Forward the tab key to the terminal, rather than moving between controls.
|
||||||
|
if( key == GLFW.GLFW_KEY_TAB && getFocused() != null && getFocused() == terminal )
|
||||||
|
{
|
||||||
|
return getFocused().keyPressed( key, scancode, modifiers );
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.keyPressed( key, scancode, modifiers );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void render( int mouseX, int mouseY, float partialTicks )
|
||||||
|
{
|
||||||
|
renderBackground();
|
||||||
|
super.render( mouseX, mouseY, partialTicks );
|
||||||
|
renderTooltip( mouseX, mouseY );
|
||||||
|
|
||||||
|
for( Widget widget : buttons )
|
||||||
|
{
|
||||||
|
if( widget.isHovered() ) widget.renderToolTip( mouseX, mouseY );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final boolean mouseDragged( double x, double y, int button, double deltaX, double deltaY )
|
||||||
|
{
|
||||||
|
return (getFocused() != null && getFocused().mouseDragged( x, y, button, deltaX, deltaY ))
|
||||||
|
|| super.mouseDragged( x, y, button, deltaX, deltaY );
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,36 +10,25 @@
|
||||||
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
|
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
|
||||||
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
||||||
import dan200.computercraft.client.render.ComputerBorderRenderer;
|
import dan200.computercraft.client.render.ComputerBorderRenderer;
|
||||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
|
||||||
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
||||||
import dan200.computercraft.shared.computer.inventory.ContainerComputerBase;
|
import dan200.computercraft.shared.computer.inventory.ContainerComputerBase;
|
||||||
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
||||||
import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
|
import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
|
||||||
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
|
|
||||||
import net.minecraft.client.gui.widget.Widget;
|
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
import net.minecraft.util.text.ITextComponent;
|
import net.minecraft.util.text.ITextComponent;
|
||||||
import org.lwjgl.glfw.GLFW;
|
|
||||||
|
|
||||||
import static dan200.computercraft.client.render.ComputerBorderRenderer.BORDER;
|
import static dan200.computercraft.client.render.ComputerBorderRenderer.BORDER;
|
||||||
|
|
||||||
public final class GuiComputer<T extends ContainerComputerBase> extends ContainerScreen<T>
|
public final class GuiComputer<T extends ContainerComputerBase> extends ComputerScreenBase<T>
|
||||||
{
|
{
|
||||||
private final ComputerFamily family;
|
|
||||||
private final ClientComputer computer;
|
|
||||||
private final int termWidth;
|
private final int termWidth;
|
||||||
private final int termHeight;
|
private final int termHeight;
|
||||||
|
|
||||||
private WidgetTerminal terminal = null;
|
|
||||||
|
|
||||||
private GuiComputer(
|
private GuiComputer(
|
||||||
T container, PlayerInventory player, ITextComponent title, int termWidth, int termHeight
|
T container, PlayerInventory player, ITextComponent title, int termWidth, int termHeight
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
super( container, player, title );
|
super( container, player, title, BORDER );
|
||||||
family = container.getFamily();
|
|
||||||
computer = (ClientComputer) container.getComputer();
|
|
||||||
this.termWidth = termWidth;
|
this.termWidth = termWidth;
|
||||||
this.termHeight = termHeight;
|
this.termHeight = termHeight;
|
||||||
|
|
||||||
|
@ -71,45 +60,12 @@ public static GuiComputer<ContainerViewComputer> createView( ContainerViewComput
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void init()
|
protected WidgetTerminal createTerminal()
|
||||||
{
|
{
|
||||||
super.init();
|
return new WidgetTerminal( computer,
|
||||||
|
|
||||||
minecraft.keyboardHandler.setSendRepeatsToGui( true );
|
|
||||||
|
|
||||||
terminal = addButton( new WidgetTerminal( computer,
|
|
||||||
leftPos + ComputerSidebar.WIDTH + BORDER, topPos + BORDER, termWidth, termHeight
|
leftPos + ComputerSidebar.WIDTH + BORDER, topPos + BORDER, termWidth, termHeight
|
||||||
) );
|
);
|
||||||
ComputerSidebar.addButtons( this, computer, this::addButton, leftPos, topPos + BORDER );
|
|
||||||
setFocused( terminal );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removed()
|
|
||||||
{
|
|
||||||
super.removed();
|
|
||||||
minecraft.keyboardHandler.setSendRepeatsToGui( false );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void tick()
|
|
||||||
{
|
|
||||||
super.tick();
|
|
||||||
terminal.update();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean keyPressed( int key, int scancode, int modifiers )
|
|
||||||
{
|
|
||||||
// Forward the tab key to the terminal, rather than moving between controls.
|
|
||||||
if( key == GLFW.GLFW_KEY_TAB && getFocused() != null && getFocused() == terminal )
|
|
||||||
{
|
|
||||||
return getFocused().keyPressed( key, scancode, modifiers );
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.keyPressed( key, scancode, modifiers );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -119,26 +75,6 @@ public void renderBg( float partialTicks, int mouseX, int mouseY )
|
||||||
RenderSystem.color4f( 1, 1, 1, 1 );
|
RenderSystem.color4f( 1, 1, 1, 1 );
|
||||||
minecraft.getTextureManager().bind( ComputerBorderRenderer.getTexture( family ) );
|
minecraft.getTextureManager().bind( ComputerBorderRenderer.getTexture( family ) );
|
||||||
ComputerBorderRenderer.render( terminal.x, terminal.y, getBlitOffset(), terminal.getWidth(), terminal.getHeight() );
|
ComputerBorderRenderer.render( terminal.x, terminal.y, getBlitOffset(), terminal.getWidth(), terminal.getHeight() );
|
||||||
ComputerSidebar.renderBackground( leftPos, topPos + BORDER );
|
ComputerSidebar.renderBackground( leftPos, topPos + sidebarYOffset );
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void render( int mouseX, int mouseY, float partialTicks )
|
|
||||||
{
|
|
||||||
renderBackground();
|
|
||||||
super.render( mouseX, mouseY, partialTicks );
|
|
||||||
renderTooltip( mouseX, mouseY );
|
|
||||||
|
|
||||||
for( Widget widget : buttons )
|
|
||||||
{
|
|
||||||
if( widget.isHovered() ) widget.renderToolTip( mouseX, mouseY );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean mouseDragged( double x, double y, int button, double deltaX, double deltaY )
|
|
||||||
{
|
|
||||||
return (getFocused() != null && getFocused().mouseDragged( x, y, button, deltaX, deltaY ))
|
|
||||||
|| super.mouseDragged( x, y, button, deltaX, deltaY );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,80 +10,40 @@
|
||||||
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
|
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
|
||||||
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
|
||||||
import dan200.computercraft.client.render.ComputerBorderRenderer;
|
import dan200.computercraft.client.render.ComputerBorderRenderer;
|
||||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
|
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
|
||||||
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
|
|
||||||
import net.minecraft.client.gui.widget.Widget;
|
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraft.util.text.ITextComponent;
|
import net.minecraft.util.text.ITextComponent;
|
||||||
import org.lwjgl.glfw.GLFW;
|
|
||||||
|
|
||||||
import static dan200.computercraft.shared.turtle.inventory.ContainerTurtle.*;
|
import static dan200.computercraft.shared.turtle.inventory.ContainerTurtle.*;
|
||||||
|
|
||||||
public class GuiTurtle extends ContainerScreen<ContainerTurtle>
|
public class GuiTurtle extends ComputerScreenBase<ContainerTurtle>
|
||||||
{
|
{
|
||||||
private static final ResourceLocation BACKGROUND_NORMAL = new ResourceLocation( ComputerCraft.MOD_ID, "textures/gui/turtle_normal.png" );
|
private static final ResourceLocation BACKGROUND_NORMAL = new ResourceLocation( ComputerCraft.MOD_ID, "textures/gui/turtle_normal.png" );
|
||||||
private static final ResourceLocation BACKGROUND_ADVANCED = new ResourceLocation( ComputerCraft.MOD_ID, "textures/gui/turtle_advanced.png" );
|
private static final ResourceLocation BACKGROUND_ADVANCED = new ResourceLocation( ComputerCraft.MOD_ID, "textures/gui/turtle_advanced.png" );
|
||||||
|
|
||||||
private final ContainerTurtle container;
|
private static final int TEX_WIDTH = 254;
|
||||||
|
private static final int TEX_HEIGHT = 217;
|
||||||
|
|
||||||
private final ComputerFamily family;
|
private final ComputerFamily family;
|
||||||
private final ClientComputer computer;
|
|
||||||
|
|
||||||
private WidgetTerminal terminal;
|
|
||||||
|
|
||||||
public GuiTurtle( ContainerTurtle container, PlayerInventory player, ITextComponent title )
|
public GuiTurtle( ContainerTurtle container, PlayerInventory player, ITextComponent title )
|
||||||
{
|
{
|
||||||
super( container, player, title );
|
super( container, player, title, BORDER );
|
||||||
|
|
||||||
this.container = container;
|
|
||||||
family = container.getFamily();
|
family = container.getFamily();
|
||||||
computer = (ClientComputer) container.getComputer();
|
|
||||||
|
|
||||||
imageWidth = 254;
|
imageWidth = TEX_WIDTH + ComputerSidebar.WIDTH;
|
||||||
imageHeight = 217;
|
imageHeight = TEX_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void init()
|
protected WidgetTerminal createTerminal()
|
||||||
{
|
{
|
||||||
super.init();
|
return new WidgetTerminal(
|
||||||
minecraft.keyboardHandler.setSendRepeatsToGui( true );
|
|
||||||
|
|
||||||
terminal = addButton( new WidgetTerminal(
|
|
||||||
computer, leftPos + BORDER + ComputerSidebar.WIDTH, topPos + BORDER,
|
computer, leftPos + BORDER + ComputerSidebar.WIDTH, topPos + BORDER,
|
||||||
ComputerCraft.turtleTermWidth, ComputerCraft.turtleTermHeight
|
ComputerCraft.turtleTermWidth, ComputerCraft.turtleTermHeight
|
||||||
) );
|
);
|
||||||
ComputerSidebar.addButtons( this, computer, this::addButton, leftPos, topPos + BORDER );
|
|
||||||
setFocused( terminal );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removed()
|
|
||||||
{
|
|
||||||
super.removed();
|
|
||||||
minecraft.keyboardHandler.setSendRepeatsToGui( false );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void tick()
|
|
||||||
{
|
|
||||||
super.tick();
|
|
||||||
terminal.update();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean keyPressed( int key, int scancode, int modifiers )
|
|
||||||
{
|
|
||||||
// Forward the tab key to the terminal, rather than moving between controls.
|
|
||||||
if( key == GLFW.GLFW_KEY_TAB && getFocused() != null && getFocused() == terminal )
|
|
||||||
{
|
|
||||||
return getFocused().keyPressed( key, scancode, modifiers );
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.keyPressed( key, scancode, modifiers );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -93,12 +53,12 @@ protected void renderBg( float partialTicks, int mouseX, int mouseY )
|
||||||
|
|
||||||
RenderSystem.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
RenderSystem.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||||
minecraft.getTextureManager().bind( advanced ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL );
|
minecraft.getTextureManager().bind( advanced ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL );
|
||||||
blit( leftPos + ComputerSidebar.WIDTH, topPos, 0, 0, imageWidth, imageHeight );
|
blit( leftPos + ComputerSidebar.WIDTH, topPos, 0, 0, TEX_WIDTH, TEX_HEIGHT );
|
||||||
|
|
||||||
minecraft.getTextureManager().bind( advanced ? ComputerBorderRenderer.BACKGROUND_ADVANCED : ComputerBorderRenderer.BACKGROUND_NORMAL );
|
minecraft.getTextureManager().bind( advanced ? ComputerBorderRenderer.BACKGROUND_ADVANCED : ComputerBorderRenderer.BACKGROUND_NORMAL );
|
||||||
ComputerSidebar.renderBackground( leftPos, topPos + BORDER );
|
ComputerSidebar.renderBackground( leftPos, topPos + sidebarYOffset );
|
||||||
|
|
||||||
int slot = container.getSelectedSlot();
|
int slot = getMenu().getSelectedSlot();
|
||||||
if( slot >= 0 )
|
if( slot >= 0 )
|
||||||
{
|
{
|
||||||
RenderSystem.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
RenderSystem.color4f( 1.0F, 1.0F, 1.0F, 1.0F );
|
||||||
|
@ -110,24 +70,4 @@ protected void renderBg( float partialTicks, int mouseX, int mouseY )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void render( int mouseX, int mouseY, float partialTicks )
|
|
||||||
{
|
|
||||||
renderBackground();
|
|
||||||
super.render( mouseX, mouseY, partialTicks );
|
|
||||||
renderTooltip( mouseX, mouseY );
|
|
||||||
|
|
||||||
for( Widget widget : buttons )
|
|
||||||
{
|
|
||||||
if( widget.isHovered() ) widget.renderToolTip( mouseX, mouseY );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean mouseDragged( double x, double y, int button, double deltaX, double deltaY )
|
|
||||||
{
|
|
||||||
return (getFocused() != null && getFocused().mouseDragged( x, y, button, deltaX, deltaY ))
|
|
||||||
|| super.mouseDragged( x, y, button, deltaX, deltaY );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -169,8 +169,8 @@ public boolean mouseClicked( double mouseX, double mouseY, int button )
|
||||||
Terminal term = computer.getTerminal();
|
Terminal term = computer.getTerminal();
|
||||||
if( term != null )
|
if( term != null )
|
||||||
{
|
{
|
||||||
int charX = (int) ((mouseX - x) / FONT_WIDTH);
|
int charX = (int) ((mouseX - innerX) / FONT_WIDTH);
|
||||||
int charY = (int) ((mouseY - x) / FONT_HEIGHT);
|
int charY = (int) ((mouseY - innerY) / FONT_HEIGHT);
|
||||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||||
|
|
||||||
|
@ -193,8 +193,8 @@ public boolean mouseReleased( double mouseX, double mouseY, int button )
|
||||||
Terminal term = computer.getTerminal();
|
Terminal term = computer.getTerminal();
|
||||||
if( term != null )
|
if( term != null )
|
||||||
{
|
{
|
||||||
int charX = (int) ((mouseX - x) / FONT_WIDTH);
|
int charX = (int) ((mouseX - innerX) / FONT_WIDTH);
|
||||||
int charY = (int) ((mouseY - x) / FONT_HEIGHT);
|
int charY = (int) ((mouseY - innerY) / FONT_HEIGHT);
|
||||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||||
|
|
||||||
|
@ -220,8 +220,8 @@ public boolean mouseDragged( double mouseX, double mouseY, int button, double v2
|
||||||
Terminal term = computer.getTerminal();
|
Terminal term = computer.getTerminal();
|
||||||
if( term != null )
|
if( term != null )
|
||||||
{
|
{
|
||||||
int charX = (int) ((mouseX - x) / FONT_WIDTH);
|
int charX = (int) ((mouseX - innerX) / FONT_WIDTH);
|
||||||
int charY = (int) ((mouseY - x) / FONT_HEIGHT);
|
int charY = (int) ((mouseY - innerY) / FONT_HEIGHT);
|
||||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue