From 96319b27277672f82e452b81a6ce3cb722e53746 Mon Sep 17 00:00:00 2001 From: yvt Date: Fri, 1 Nov 2013 00:39:17 +0900 Subject: [PATCH] Support for drawing (or not drawing) the cross hair from scripts --- .../Scripts/Reference/Skin/IViewToolSkin.as | 3 ++ Resources/Scripts/Skin/BasicViewWeapon.as | 16 +++++++++- Resources/Scripts/Skin/Block/View.as | 10 +++++++ Resources/Scripts/Skin/Grenade/View.as | 9 ++++++ Resources/Scripts/Skin/Rifle/View.as | 5 ++-- Resources/Scripts/Skin/SMG/View.as | 6 ++-- Resources/Scripts/Skin/Shotgun/View.as | 5 ++-- Resources/Scripts/Skin/Spade/View.as | 10 +++++++ Sources/Client/ClientPlayer.cpp | 30 +++++++++++++++++++ Sources/Client/ClientPlayer.h | 1 + Sources/ScriptBindings/IViewToolSkin.cpp | 16 +++++++++- Sources/ScriptBindings/IViewToolSkin.h | 1 + 12 files changed, 101 insertions(+), 11 deletions(-) diff --git a/Resources/Scripts/Reference/Skin/IViewToolSkin.as b/Resources/Scripts/Reference/Skin/IViewToolSkin.as index 76ea2986..e4b6fff5 100644 --- a/Resources/Scripts/Reference/Skin/IViewToolSkin.as +++ b/Resources/Scripts/Reference/Skin/IViewToolSkin.as @@ -33,6 +33,9 @@ namespace spades { /** Returns positions for player hands in view coordinate. */ Vector3 LeftHandPosition { get; } Vector3 RightHandPosition { get; } + + /** Issues draw commands to draw 2d images, such as HUD and crosshair. */ + void Draw2D(); } } diff --git a/Resources/Scripts/Skin/BasicViewWeapon.as b/Resources/Scripts/Skin/BasicViewWeapon.as index bcd5bd0e..3e5d42f4 100644 --- a/Resources/Scripts/Skin/BasicViewWeapon.as +++ b/Resources/Scripts/Skin/BasicViewWeapon.as @@ -47,6 +47,8 @@ namespace spades { get { return muted; } } + + // IWeaponSkin private float aimDownSightState; @@ -127,9 +129,14 @@ namespace spades { } } + private Renderer@ renderer; + private Image@ sightImage; - BasicViewWeapon() { + BasicViewWeapon(Renderer@ renderer) { + @this.renderer = renderer; localFireVibration = 0.f; + @sightImage = renderer.RegisterImage + ("Gfx/Sight.tga"); } float GetLocalFireVibration() { @@ -205,6 +212,13 @@ namespace spades { void ReloadedWeapon() { } + + void Draw2D() { + renderer.Color = (Vector4(1.f, 1.f, 1.f, 1.f)); + renderer.DrawImage(sightImage, + Vector2((renderer.ScreenWidth - sightImage.Width) * 0.5f, + (renderer.ScreenHeight - sightImage.Height) * 0.5f)); + } } } diff --git a/Resources/Scripts/Skin/Block/View.as b/Resources/Scripts/Skin/Block/View.as index a48c46dd..d81c9291 100644 --- a/Resources/Scripts/Skin/Block/View.as +++ b/Resources/Scripts/Skin/Block/View.as @@ -79,12 +79,15 @@ private Renderer@ renderer; private AudioDevice@ audioDevice; private Model@ model; + private Image@ sightImage; ViewBlockSkin(Renderer@ r, AudioDevice@ dev) { @renderer = r; @audioDevice = dev; @model = renderer.RegisterModel ("Models/Weapons/Block/Block2.kv6"); + @sightImage = renderer.RegisterImage + ("Gfx/Sight.tga"); } void Update(float dt) { @@ -122,6 +125,13 @@ param.depthHack = true; renderer.AddModel(model, param); } + + void Draw2D() { + renderer.Color = (Vector4(1.f, 1.f, 1.f, 1.f)); + renderer.DrawImage(sightImage, + Vector2((renderer.ScreenWidth - sightImage.Width) * 0.5f, + (renderer.ScreenHeight - sightImage.Height) * 0.5f)); + } } IBlockSkin@ CreateViewBlockSkin(Renderer@ r, AudioDevice@ dev) { diff --git a/Resources/Scripts/Skin/Grenade/View.as b/Resources/Scripts/Skin/Grenade/View.as index d41fecc7..452d15cd 100644 --- a/Resources/Scripts/Skin/Grenade/View.as +++ b/Resources/Scripts/Skin/Grenade/View.as @@ -79,12 +79,15 @@ private Renderer@ renderer; private AudioDevice@ audioDevice; private Model@ model; + private Image@ sightImage; ViewGrenadeSkin(Renderer@ r, AudioDevice@ dev) { @renderer = r; @audioDevice = dev; @model = renderer.RegisterModel ("Models/Weapons/Grenade/Grenade.kv6"); + @sightImage = renderer.RegisterImage + ("Gfx/Sight.tga"); } void Update(float dt) { @@ -155,6 +158,12 @@ } } + void Draw2D() { + renderer.Color = (Vector4(1.f, 1.f, 1.f, 1.f)); + renderer.DrawImage(sightImage, + Vector2((renderer.ScreenWidth - sightImage.Width) * 0.5f, + (renderer.ScreenHeight - sightImage.Height) * 0.5f)); + } } IGrenadeSkin@ CreateViewGrenadeSkin(Renderer@ r, AudioDevice@ dev) { diff --git a/Resources/Scripts/Skin/Rifle/View.as b/Resources/Scripts/Skin/Rifle/View.as index cb6f2024..af9d00e7 100644 --- a/Resources/Scripts/Skin/Rifle/View.as +++ b/Resources/Scripts/Skin/Rifle/View.as @@ -23,7 +23,6 @@ IToolSkin, IViewToolSkin, IWeaponSkin, BasicViewWeapon { - private Renderer@ renderer; private AudioDevice@ audioDevice; private Model@ gunModel; private Model@ magazineModel; @@ -33,8 +32,8 @@ private AudioChunk@ fireStereoSound; private AudioChunk@ reloadSound; - ViewRifleSkin(Renderer@ r, AudioDevice@ dev) { - @renderer = r; + ViewRifleSkin(Renderer@ r, AudioDevice@ dev){ + super(r); @audioDevice = dev; @gunModel = renderer.RegisterModel ("Models/Weapons/Rifle/WeaponNoMagazine.kv6"); diff --git a/Resources/Scripts/Skin/SMG/View.as b/Resources/Scripts/Skin/SMG/View.as index 87a4524f..25226d5b 100644 --- a/Resources/Scripts/Skin/SMG/View.as +++ b/Resources/Scripts/Skin/SMG/View.as @@ -23,7 +23,6 @@ IToolSkin, IViewToolSkin, IWeaponSkin, BasicViewWeapon { - private Renderer@ renderer; private AudioDevice@ audioDevice; private Model@ gunModel; private Model@ magazineModel; @@ -33,8 +32,8 @@ private AudioChunk@ fireStereoSound; private AudioChunk@ reloadSound; - ViewSMGSkin(Renderer@ r, AudioDevice@ dev) { - @renderer = r; + ViewSMGSkin(Renderer@ r, AudioDevice@ dev){ + super(r); @audioDevice = dev; @gunModel = renderer.RegisterModel ("Models/Weapons/SMG/WeaponNoMagazine.kv6"); @@ -152,6 +151,7 @@ LeftHandPosition = leftHand; RightHandPosition = rightHand; } + } IWeaponSkin@ CreateViewSMGSkin(Renderer@ r, AudioDevice@ dev) { diff --git a/Resources/Scripts/Skin/Shotgun/View.as b/Resources/Scripts/Skin/Shotgun/View.as index 4e578022..103ee51a 100644 --- a/Resources/Scripts/Skin/Shotgun/View.as +++ b/Resources/Scripts/Skin/Shotgun/View.as @@ -23,7 +23,6 @@ IToolSkin, IViewToolSkin, IWeaponSkin, BasicViewWeapon { - private Renderer@ renderer; private AudioDevice@ audioDevice; private Model@ gunModel; private Model@ pumpModel; @@ -34,8 +33,8 @@ private AudioChunk@ reloadSound; private AudioChunk@ cockSound; - ViewShotgunSkin(Renderer@ r, AudioDevice@ dev) { - @renderer = r; + ViewShotgunSkin(Renderer@ r, AudioDevice@ dev){ + super(r); @audioDevice = dev; @gunModel = renderer.RegisterModel ("Models/Weapons/Shotgun/WeaponNoPump.kv6"); diff --git a/Resources/Scripts/Skin/Spade/View.as b/Resources/Scripts/Skin/Spade/View.as index daa5ae24..27d39a36 100644 --- a/Resources/Scripts/Skin/Spade/View.as +++ b/Resources/Scripts/Skin/Spade/View.as @@ -79,12 +79,15 @@ private Renderer@ renderer; private AudioDevice@ audioDevice; private Model@ model; + private Image@ sightImage; ViewSpadeSkin(Renderer@ r, AudioDevice@ dev) { @renderer = r; @audioDevice = dev; @model = renderer.RegisterModel ("Models/Weapons/Spade/Spade.kv6"); + @sightImage = renderer.RegisterImage + ("Gfx/Sight.tga"); } void Update(float dt) { @@ -168,6 +171,13 @@ param.depthHack = true; renderer.AddModel(model, param); } + + void Draw2D() { + renderer.Color = (Vector4(1.f, 1.f, 1.f, 1.f)); + renderer.DrawImage(sightImage, + Vector2((renderer.ScreenWidth - sightImage.Width) * 0.5f, + (renderer.ScreenHeight - sightImage.Height) * 0.5f)); + } } ISpadeSkin@ CreateViewSpadeSkin(Renderer@ r, AudioDevice@ dev) { diff --git a/Sources/Client/ClientPlayer.cpp b/Sources/Client/ClientPlayer.cpp index e2f5c3b1..5485c059 100644 --- a/Sources/Client/ClientPlayer.cpp +++ b/Sources/Client/ClientPlayer.cpp @@ -880,6 +880,36 @@ namespace spades { } } + void ClientPlayer::Draw2D() { + if(!ShouldRenderInThirdPersonView()) { + asIScriptObject *skin; + + if(currentTool == Player::ToolSpade) { + skin = spadeViewSkin; + }else if(currentTool == Player::ToolBlock) { + skin = blockViewSkin; + }else if(currentTool == Player::ToolGrenade) { + skin = grenadeViewSkin; + }else if(currentTool == Player::ToolWeapon) { + skin = weaponViewSkin; + }else{ + SPInvalidEnum("currentTool", currentTool); + } + + SetSkinParameterForTool(currentTool, skin); + + SetCommonSkinParameter(skin); + + // common process + { + ScriptIViewToolSkin interface(skin); + interface.SetEyeMatrix(GetEyeMatrix()); + interface.SetSwing(viewWeaponOffset); + interface.Draw2D(); + } + } + } + bool ClientPlayer::ShouldRenderInThirdPersonView() { if(player != player->GetWorld()->GetLocalPlayer()) return true; diff --git a/Sources/Client/ClientPlayer.h b/Sources/Client/ClientPlayer.h index 30ec214f..e19e2ce4 100644 --- a/Sources/Client/ClientPlayer.h +++ b/Sources/Client/ClientPlayer.h @@ -76,6 +76,7 @@ namespace spades { void Update(float dt); void AddToScene(); + void Draw2D(); bool IsChangingTool(); void FiredWeapon(); diff --git a/Sources/ScriptBindings/IViewToolSkin.cpp b/Sources/ScriptBindings/IViewToolSkin.cpp index 5e291fc8..30a25554 100644 --- a/Sources/ScriptBindings/IViewToolSkin.cpp +++ b/Sources/ScriptBindings/IViewToolSkin.cpp @@ -76,6 +76,17 @@ namespace spades{ return *reinterpret_cast(ctx->GetReturnObject()); } + void ScriptIViewToolSkin::Draw2D() { + SPADES_MARK_FUNCTION_DEBUG(); + static ScriptFunction func("IViewToolSkin", + "void Draw2D()"); + ScriptContextHandle ctx = func.Prepare(); + int r; + r = ctx->SetObject((void *)obj); + ScriptManager::CheckError(r); + ctx.ExecuteChecked(); + } + class IViewToolSkinRegistrar: public ScriptObjectRegistrar { public: IViewToolSkinRegistrar(): @@ -105,7 +116,10 @@ namespace spades{ manager->CheckError(r); r = eng->RegisterInterfaceMethod("IViewToolSkin", "Vector3 get_RightHandPosition()"); - manager->CheckError(r); + manager->CheckError(r); + r = eng->RegisterInterfaceMethod("IViewToolSkin", + "void Draw2D()"); + manager->CheckError(r); break; default: diff --git a/Sources/ScriptBindings/IViewToolSkin.h b/Sources/ScriptBindings/IViewToolSkin.h index 2018628d..6c735718 100644 --- a/Sources/ScriptBindings/IViewToolSkin.h +++ b/Sources/ScriptBindings/IViewToolSkin.h @@ -33,6 +33,7 @@ namespace spades { void SetSwing(Vector3); Vector3 GetLeftHandPosition(); Vector3 GetRightHandPosition(); + void Draw2D(); }; } } \ No newline at end of file