JavaFX 8.0 - How can I get a MouseEventHandle at title bar - mouseevent

I have implemented a EventHandler which gets the window-drag of the mouse:
public void mouseEventHandler(){
final Delta dragDelta = new Delta();
view.getScene().setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
dragDelta.x = mainStage.getX() - mouseEvent.getScreenX();
dragDelta.y = mainStage.getY() - mouseEvent.getScreenY();
}
});
view.getScene().setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
mainStage.setX(mouseEvent.getScreenX() + dragDelta.x);
mainStage.setY(mouseEvent.getScreenY() + dragDelta.y);
stage.setX(mainStage.getX());
stage.setY(mainStage.getY() + view.getScene().getHeight() + 25);
}
});
}
I have two stages: mainStage and a stage, which is addicted to the coordinates of the mainStage. My problem is, that the stage only moves if I click in the window of the mainStage and drag it. But it won't move if I click the title bar of the window for removing.
How can I get the MouseEvent for the title bar?
Here is the show() method, so you can understand the Addiction:
public void show(Stage mainStage, MenuPresenter menuPresenter){
this.model = new Model();
this.view = new GameView(this.model);
this.chatWatchView = new ChatWatchView();
this.score = this.model.getScore();
this.menuPresenter = menuPresenter;
this.mainStage = mainStage;
mainStage.setScene(view.getScene());
mainStage.centerOnScreen();
stage = new Stage();
stage.setScene(chatWatchView.getScene());
stage.initStyle(StageStyle.UNDECORATED);
stage.setX(mainStage.getX());
stage.setY(mainStage.getY() + view.getScene().getHeight() + 25);
stage.show();
mainStage.show();
startTimeline();
activateEventHandler();
mouseEventHandler();
//view.setScore("Score: " + model.getScore());
}

Had similar problem. Consider to add listeners to xProperty() and yProperty() of your Stage, I think it will work better.
In my case I ended up with something like this
void updatePosition(double stageX, double stageY) {
//...
}
And then
stage.xProperty().addListener(((observable, oldValue, newValue) -> {
updatePosition(newValue, stage.getY())
}));
stage.yProperty().addListener(((observable, oldValue, newValue) -> {
updatePosition(stage.getX(), newValue)
}));

Related

MvxRecyclerView ItemDecoration not working in Marshmallow

I am trying to use MvxRecyclerView. I have directly binded the ItemSource from the ViewModel and its redering the list. But i want the ListItemDivider in between,. So i made a custom ItemDecorator and used it with the recycler view. Its working for andorid 7.0 but not for marshmallow. I m not using any Holder, LinearLayoutManager or Adapter separately for the MvxRecyclerView.Please find the code below
Activity class
//****************************************************************************//
[Activity(Label = "EmployeeHub", Theme = "#style/MaterialTheme", ScreenOrientation = AndroidSystem.Content.PM.ScreenOrientation.Portrait)]
public class EmployeeHub : BaseView
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.EmployeeHub);
var reclycleView = this.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
AndroidSystem.Support.V7.Widget.RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecoration(this.ApplicationContext);
reclycleView.AddItemDecoration(dividerItemDecoration);
}
}
ItemDecorator
//********************************************************************//
public class DividerItemDecoration : RecyclerView.ItemDecoration
{
private AndroidSystem.Graphics.Drawables.Drawable divider;
private int[] attributes = new int[] {Attribute.ListDivider };
public DividerItemDecoration(Context context)
{
divider = context.GetDrawable(Resource.Drawable.noBorder);
TypedArray ta = context.ObtainStyledAttributes(attributes);
divider = ta.GetDrawable(0);
ta.Recycle();
}
public override void GetItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
{
if (parent.GetChildAdapterPosition(view) == 0)
{
return;
}
outRect.Top = divider.IntrinsicHeight;
}
public override void OnDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
{
int left = parent.PaddingLeft;
int right = parent.Width - parent.PaddingRight;
for (int i = 0; i < parent.ChildCount; i++)
{
View child = parent.GetChildAt(i);
var parameters = child.LayoutParameters.JavaCast<RecyclerView.LayoutParams>();
int top = child.Bottom + parameters.BottomMargin;
int bottom = top + divider.IntrinsicHeight;
divider.SetBounds(left, top, right, bottom);
divider.Draw(c);
}
}
}
// layout **********************************************//
<MvxRecyclerView
android:id="#+id/my_recycler_view"
android:layout_below="#+id/hlhrview1"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxItemTemplate="#layout/employee_item_template"
local:MvxBind="ItemsSource Employees; ItemClick SelectedEmployee" />
I also had a problem getting this to work. With a tip from here and a few modifications to your code, I was able to get it working. Specifically, I am using the OnDrawOver as opposed to OnDraw
Here is my modified DividerItemDecoration:
public class DividerItemDecoration : RecyclerView.ItemDecoration
{
private readonly Drawable _divider;
public DividerItemDecoration(Context context)
{
_divider = ResourcesCompat.GetDrawable(context.Resources, Resource.Drawable.recyclerview_line_divider, context.Theme);
}
public override void OnDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
{
var left = parent.PaddingLeft;
var right = parent.Width - parent.PaddingRight;
for (var i = 0; i < parent.ChildCount; i++)
{
var child = parent.GetChildAt(i);
var parameters = child.LayoutParameters.JavaCast<RecyclerView.LayoutParams>();
var top = child.Bottom + parameters.BottomMargin;
var bottom = top + _divider.IntrinsicHeight;
_divider.SetBounds(left, top, right, bottom);
_divider.Draw(c);
}
}
}
I also put the binding code in my OnCreateView like this:
var reclycleView = _payformView.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecoration(Application.Context);
reclycleView.AddItemDecoration(dividerItemDecoration);
All this is on a KitKat device.

Xamarin Forms Switch XAML

I'm new in Xamarin and i'm trying to create a simple page with some components.
One of these component is a Switch it works fine by itself but i would like to change the basic text "inactive/active" by "male/female"
I've seen that in Xaml for windows phone there is a ToggleSwitch Component with a On/OffContent property but i can't seems to find an equivalent in XAML for Xamarin Forms
any idea ?
Thank you!
The lack of built in switch options, or at least the lack of being able to rename the switch options, has been asked a few times.
You could go with custom renders, modify the text at the OS level or do like I chose to do, just build your own switch.
This switch is two buttons laid out horizontally with the text Yes and No. The selected button gets a red border, and the unselected a transparent border.
class CustomSwitch : Grid
{
public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
private Button negative;
private Button positive;
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create<CustomSwitch, Object>(t => t.SelectedItem, null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);
public CustomSwitch()
{
try
{
this.HorizontalOptions = LayoutOptions.Center;
this.VerticalOptions = LayoutOptions.Center;
negative = new Button();
negative.Text = "No";
negative.Style = <YourNameSpace>.AppStyling.Style_Button_Switch;
negative.Clicked += (o,s) => OnSelectedItemChanged(this, ItemSelected, (int)Classes.Collections.Enums.SelectionStatus.False);
positive = new Button();
positive.Text = "Yes";
positive.Style = <YourNameSpace>.AppStyling.Style_Button_Switch;
positive.Clicked += (o, s) => OnSelectedItemChanged(this, ItemSelected, (int)Classes.Collections.Enums.SelectionStatus.True);
this.Children.Add(negative, 0,0);
this.Children.Add(positive, 1,0);
}
catch(System.Exception ex)
{
<YourNameSpace>.Classes.Helpers.Helper_ErrorHandling.SendErrorToServer(ex, this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name);
}
}
public Object SelectedItem
{
get
{
return base.GetValue(SelectedItemProperty);
}
set
{
if (SelectedItem != value)
{
base.SetValue(SelectedItemProperty, value);
InternalUpdateSelected();
}
}
}
private void InternalUpdateSelected()
{
if((int)SelectedItem == (int)Classes.Collections.Enums.SelectionStatus.False)
{
negative.BorderColor = <YourNameSpace>.AppStyling.Color_Selected;
positive.BorderColor = <YourNameSpace>.AppStyling.Color_UnSelected;
positive.Opacity = <YourNameSpace>.AppStyling.Opaque_High;
}
else if ((int)SelectedItem == (int)Classes.Collections.Enums.SelectionStatus.True)
{
negative.BorderColor = <YourNameSpace>.AppStyling.Color_UnSelected;
negative.Opacity = <YourNameSpace>.AppStyling.Opaque_High;
positive.BorderColor = <YourNameSpace>.AppStyling.Color_Selected;
}
else
{
negative.BorderColor = <YourNameSpace>.AppStyling.Color_UnSelected;
negative.Opacity = <YourNameSpace>.AppStyling.Opaque_High;
positive.BorderColor = <YourNameSpace>.AppStyling.Color_UnSelected;
positive.Opacity = <YourNameSpace>.AppStyling.Opaque_High;
}
}
private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
{
CustomSwitch boundSwitch = (CustomSwitch)bindable;
if((int)newValue != (int)Classes.Collections.Enums.SelectionStatus.Unselected)
{
boundSwitch.SelectedItem = (int)newValue == (int)Classes.Collections.Enums.SelectionStatus.False ? (int)Classes.Collections.Enums.SelectionStatus.False : (int)Classes.Collections.Enums.SelectionStatus.True;
}
if (boundSwitch.ItemSelected != null)
{
boundSwitch.ItemSelected(boundSwitch, new SelectedItemChangedEventArgs(newValue));
}
boundSwitch.InternalUpdateSelected();
}
}

invisible view toolbar action

I wanted to make invisible some action added to the view toolbar based on perspective change.
Following is my code for making a particular action invisible. This code does not give me any effect.
The code executes fine, but it does not disappear from the toolbar.
IContributionItem[] items = view.getToolbar().getItems();
for (IContributionItem item : items) {
if (item instanceof ActionContributionItem) {
ActionContributionItem actionItem = (ActionContributionItem) item;
if(actionItem.getAction().getId().equals("actionid")){
//view.getToolbar().remove("actionid");
actionItem.setVisible(false);
}
}
}
You need to call the update() method on the toolbar manager.
See this snippet where an item gets invisible after 5 seconds:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final ToolBarManager toolBarManager = new ToolBarManager();
final Action action1 = new Action("1") {
};
action1.setId("1");
toolBarManager.add(action1);
final Action action2 = new Action("2") {
};
action2.setId("2");
toolBarManager.add(action2);
toolBarManager.createControl(shell);
display.timerExec(5000, new Runnable() {
#Override
public void run() {
final IContributionItem[] items = toolBarManager.getItems();
for (IContributionItem item : items) {
if (item.getId().equals("1")){
item.setVisible(false);
}
}
toolBarManager.update(true);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

How can I detect when the XAML Slider is Completed?

In XAML I have the <Slider />. It has the ValueChanged event. This event fires with every change to Value. I need to detect when the value change is over. LostFocus, PointerReleased are not the correct event. How can I detect this?
XAML, WinRT, Windows8.1 and UWP:
PointerCaptureLost event should work for mouse / touch
KeyUp event for keyboard
You can create a new class and inherit from Slider. From there on, you can look for the Thumb control & listen for the events you want.
Something like this should work:
public class SliderValueChangeCompletedEventArgs : RoutedEventArgs
{
private readonly double _value;
public double Value { get { return _value; } }
public SliderValueChangeCompletedEventArgs(double value)
{
_value = value;
}
}
public delegate void SlideValueChangeCompletedEventHandler(object sender, SliderValueChangeCompletedEventArgs args);
public class ExtendedSlider : Slider
{
public event SlideValueChangeCompletedEventHandler ValueChangeCompleted;
private bool _dragging = false;
protected void OnValueChangeCompleted(double value)
{
if (ValueChangeCompleted != null)
{
ValueChangeCompleted(this, new SliderValueChangeCompletedEventArgs(value) );
}
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
var thumb = base.GetTemplateChild("HorizontalThumb") as Thumb;
if (thumb != null)
{
thumb.DragStarted += ThumbOnDragStarted;
thumb.DragCompleted += ThumbOnDragCompleted;
}
thumb = base.GetTemplateChild("VerticalThumb") as Thumb;
if (thumb != null)
{
thumb.DragStarted += ThumbOnDragStarted;
thumb.DragCompleted += ThumbOnDragCompleted;
}
}
private void ThumbOnDragCompleted(object sender, DragCompletedEventArgs e)
{
_dragging = false;
OnValueChangeCompleted(this.Value);
}
private void ThumbOnDragStarted(object sender, DragStartedEventArgs e)
{
_dragging = true;
}
protected override void OnValueChanged(double oldValue, double newValue)
{
base.OnValueChanged(oldValue, newValue);
if (!_dragging)
{
OnValueChangeCompleted(newValue);
}
}
}
You can use pair of bool values isValueChanged and (if possible change value without manipulation of pointer
) isPressed;
private void Slider_ValueChanged(object s, RangeBaseValueChangedEventArgs e) {
if (!isPressed) {
AcceptChanges();
} else {
isValueChanged = true;
}
}
Initialization code:
Window.Current.CoreWindow.PointerPressed += (e, a) => { isPressed = true; };
Window.Current.CoreWindow.PointerReleased += (e, a) => {
isPressed = false;
if (isValueChanged) AcceptChanges();
};
I had a similar issue using a Slider on Windows8/WinRT.
My problem was the following: I was reacting to the ValueChanged Event and performing a long lasting operation (writing asynchronously to a file) after each trigger. And thus running into a concurrent editing exception. In order to avoid this, I used a DispatcherTimer.
//Class member
private DispatcherTimer myDispatcherTimer = null;
private void OnSliderValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
//I update my UI right away
...
//If the dispatcher is already created, stop it
if (myDispatcherTimer!= null)
myDispatcherTimer.Stop();
//Overwrite the DispatcherTimer and thus reset the countdown
myDispatcherTimer= new DispatcherTimer();
myDispatcherTimer.Tick += (sender, o) => DoSomethingAsync();
myDispatcherTimer.Interval = new TimeSpan(0,0,2);
myDispatcherTimer.Start();
}
private async void DoSomethingAsync()
{
await DoThatLongSaveOperation();
}
You cannot directly detect what the final value is, but you can at least delay the operation until there is a long pause between two updates (e.g. in my case, if the user drags the slider and stops while maintaining the drag for 2 seconds, the save operation will be fired anyway).

Why are my screenshots only black?

could someone tell me why my screenshots are only black? I am still learning and couldnt find a clue why they are only black.
This is my Utility class
static class XNAUtilities
{
private static RenderTarget2D ssTexture;
private static KeyboardState currentState, previousState;
private static int counter;
public static void TakeScreenShot(GraphicsDevice device, Keys theKey)
{
// Take Screenshot
currentState = Keyboard.GetState();
if (currentState.IsKeyDown(theKey) && previousState.IsKeyUp(theKey))
{
//device.SetRenderTarget(null);
PresentationParameters pparameters = device.PresentationParameters;
ssTexture = new RenderTarget2D(device, pparameters.BackBufferWidth, pparameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None); //??
FileStream fileStream = new System.IO.FileStream(#"Screenshot" + "_" + counter + ".png", System.IO.FileMode.CreateNew);
ssTexture.SaveAsPng(fileStream, pparameters.BackBufferWidth, pparameters.BackBufferHeight);
counter++;
}
previousState = currentState;
}
}
}
This is my Update and Draw from Game1.cs
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
myModelRotation += MathHelper.ToRadians(1f);
// Take a Screenshot
XNAUtilities.TakeScreenShot(GraphicsDevice, Keys.F8);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in myModel.Meshes)
{
foreach (BasicEffect effects in mesh.Effects)
{
effects.EnableDefaultLighting();
effects.World = transforms[mesh.ParentBone.Index]
* Matrix.CreateRotationY(myModelRotation)
* Matrix.CreateTranslation(myModelPosition);
effects.View = Matrix.CreateLookAt(new Vector3(200, 100, 400), Vector3.Zero, Vector3.Up);
effects.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f),
GraphicsDevice.Viewport.AspectRatio, 1, 5000);
}
mesh.Draw();
}
smileySprite.DrawSprites(GraphicsDevice, spriteBatch, new Vector2(10,10), Color.White);
base.Draw(gameTime);
}
You're not actually rendering to your render target. So you're saving the blank target.
You need to wrap your scene drawing like so:
GraphicsDevice.SetRenderTarget(ssTexture);
// Render your scene here
GraphicsDevice.SetRenderTarget(null);
// Now you can save your render target as a texture