/// <summary> /// Save button callback that will save current layout /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void saveButton_Click(object sender, RoutedEventArgs e) { StringBuilder outstr = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = true; foreach (var child in controlCanvas.Children) { if (child.GetType().Name == "SimpleSwitch") { SimpleSwitch simpleSwitch = child as SimpleSwitch; Grid grid = simpleSwitch.Content as Grid; Image icon = grid.Children[1] as Image; icon.InputBindings[0].Command = null; } } XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings)); dsm.XamlWriterMode = XamlWriterMode.Expression; XamlWriter.Save(controlCanvas, dsm); string savedControls = outstr.ToString(); if (!Directory.Exists(@"layouts")) { Directory.CreateDirectory(@"layouts"); } File.WriteAllText(@"layouts/" + currentGrid + ".xaml", savedControls); foreach (var child in controlCanvas.Children) { if (child.GetType().Name == "SimpleSwitch") { SimpleSwitch simpleSwitch = child as SimpleSwitch; Grid grid = simpleSwitch.Content as Grid; Image icon = grid.Children[1] as Image; Binding commandBinding = new Binding("SwitchClickedCommand"); BindingOperations.SetBinding(icon.InputBindings[0], InputBinding.CommandProperty, commandBinding); } } Messenger.Default.Send("save", "saveObjects"); }
/// <summary> /// This function will take care of adding the new switch element /// </summary> /// <param name="sender"></param> private void AddSwitch(object sender) { SimpleSwitch simpleSwitchControl = new SimpleSwitch(); Random RNG = new Random(); int length = 16; var rString = ""; for (var i = 0; i < length; i++) { rString += ((char)(RNG.Next(1, 26) + 64)).ToString().ToLower(); } SubscribedSwitch subscribedSwitch = new SubscribedSwitch { Id = Guid.NewGuid(), UniqueName = rString, SimpleSwitch = simpleSwitchControl, Topic = "", Clickable = true }; simpleSwitchControl.Name = subscribedSwitch.UniqueName; Grid grid = subscribedSwitch.SimpleSwitch.Content as Grid; CheckBox box = grid.Children[0] as CheckBox; Image icon = grid.Children[1] as Image; Binding commandBinding = new Binding("SwitchClickedCommand"); BindingOperations.SetBinding(icon.InputBindings[0], InputBinding.CommandProperty, commandBinding); icon.InputBindings[0].CommandParameter = subscribedSwitch.UniqueName; Binding myBinding = new Binding("Switches[" + subscribedSwitch.Id + "]"); BindingOperations.SetBinding(icon, Image.SourceProperty, myBinding); subscribedSwitches.Add(subscribedSwitch); simpleSwitchControl.MouseDoubleClick += object_MouseDoubleClick; simpleSwitchControl.VerticalAlignment = VerticalAlignment.Center; simpleSwitchControl.HorizontalAlignment = HorizontalAlignment.Center; simpleSwitchControl.Margin = new Thickness(0, 0, 0, 0); controlCanvas.Children.Add(simpleSwitchControl); Messenger.Default.Send(subscribedSwitch, "switchAdd"); }
private void doInteract() { Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, 0.5f); foreach (Collider2D col in hits) { SimpleSwitch swt = col.gameObject.GetComponent <SimpleSwitch> (); if (swt != null) { swt.ToggleSwitch(); Debug.Log(name + " interacted."); } } }
bool CheckForSimpleSwitch() { //RaycastHit2D[] hits = Physics2D.OverlapCircleAll(transform.position, 0.01f); Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, 0.01f); // Note that this will return the first BatteryNode found. The radius is small, so there should only be one found foreach (Collider2D col in hits) { if (col.gameObject.GetComponent <SimpleSwitch>() != null) { curSwitch = col.gameObject.GetComponent <SimpleSwitch>(); return(true); } } return(false); }
/// <summary> /// Load switch and add all needed bindings /// </summary> /// <param name="switchData">All needed data for the switch to load</param> private void LoadSwitch(Tuple <SimpleSwitch, string> switchData) { SimpleSwitch subSwitch = switchData.Item1; string uniqueName = switchData.Item2; SubscribedSwitch subscribedSwitch = subscribedSwitches.Find(x => x.UniqueName == uniqueName); subscribedSwitch.SimpleSwitch = subSwitch; Messenger.Default.Send(subscribedSwitch, "addBindingSwitch"); Grid grid = subscribedSwitch.SimpleSwitch.Content as Grid; CheckBox box = grid.Children[0] as CheckBox; Image icon = grid.Children[1] as Image; if (subscribedSwitch.Clickable) { Binding commandBinding = new Binding("SwitchClickedCommand"); BindingOperations.SetBinding(icon.InputBindings[0], InputBinding.CommandProperty, commandBinding); } icon.InputBindings[0].CommandParameter = subscribedSwitch.UniqueName; Messenger.Default.Send(uniqueName, "getSwitchAndChangeValue"); Binding myBinding = new Binding("Switches[" + subscribedSwitch.Id + "]"); BindingOperations.SetBinding(icon, Image.SourceProperty, myBinding); if (subscribedSwitch.State) { Messenger.Default.Send(new UpdateSwitchMessage { subscribedSwitch = subscribedSwitch, value = subscribedSwitch.OnIconUri }, "ChangeSwitchValue"); } else { Messenger.Default.Send(new UpdateSwitchMessage { subscribedSwitch = subscribedSwitch, value = subscribedSwitch.OffIconUri }, "ChangeSwitchValue"); } }
/// <summary> /// This function will load switch object when the app is opened with already configured layout /// </summary> /// <param name="simpleSwitch">The switch element that should be loaded</param> private void LoadSwitch(SimpleSwitch simpleSwitch) { SimpleSwitch switchControl = new SimpleSwitch(); switchControl.Margin = new Thickness(simpleSwitch.Margin.Left, simpleSwitch.Margin.Top, simpleSwitch.Margin.Right, simpleSwitch.Margin.Bottom); switchControl.Name = simpleSwitch.Name; Tuple <SimpleSwitch, string> switchData = new Tuple <SimpleSwitch, string>(switchControl, switchControl.Name); Messenger.Default.Send(switchData, "loadSwitch"); switchControl.MouseDoubleClick += object_MouseDoubleClick; switchControl.VerticalAlignment = VerticalAlignment.Center; switchControl.HorizontalAlignment = HorizontalAlignment.Center; controlCanvas.Children.Add(switchControl); }
/// <summary> /// This function will load the whole grid from the file saved on disk /// </summary> /// <param name="gridName">Name of the grid that should be loaded</param> void reloadGrid(string gridName) { currentGrid = gridName; if (!File.Exists(@"layouts/" + gridName + ".xaml")) { controlCanvas.Children.Clear(); return; } StreamReader sR = new StreamReader(@"layouts/" + gridName + ".xaml"); string text = sR.ReadToEnd(); sR.Close(); StringReader stringReader = new StringReader(text); XmlReader xmlReader = XmlReader.Create(stringReader); Grid grid = (Grid)XamlReader.Load(xmlReader); controlCanvas.Children.Clear(); foreach (FrameworkElement child in grid.Children) { if (child.GetType().ToString() == "HomeControler.Controls.SimpleLabel") { SimpleLabel label = CloneFrameworkElement(child) as SimpleLabel; LoadLabel(label); } else if (child.GetType().ToString() == "HomeControler.Controls.SimpleSwitch") { SimpleSwitch simpleSwitch = CloneFrameworkElement(child) as SimpleSwitch; LoadSwitch(simpleSwitch); } else if (child.GetType().ToString() == "HomeControler.Controls.SimpleCamera") { SimpleCamera simpleCamera = CloneFrameworkElement(child) as SimpleCamera; LoadCamera(simpleCamera); } } Messenger.Default.Send("reload", "getSubscribedDevices"); Messenger.Default.Send("subscribe", "subscribeToTopics"); }
public override void Update(long gameTime, NamelessGame namelessGame) { foreach (IEntity entity in RegisteredEntities) { InputComponent inputComponent = entity.GetComponentOfType <InputComponent>(); if (inputComponent != null) { var playerEntity = namelessGame.PlayerEntity; foreach (Intent intent in inputComponent.Intents) { switch (intent.Intention) { case IntentEnum.MoveUp: case IntentEnum.MoveDown: case IntentEnum.MoveLeft: case IntentEnum.MoveRight: case IntentEnum.MoveTopLeft: case IntentEnum.MoveTopRight: case IntentEnum.MoveBottomLeft: case IntentEnum.MoveBottomRight: { Position position = playerEntity.GetComponentOfType <Position>(); var actionPoints = playerEntity.GetComponentOfType <ActionPoints>(); if (position != null && actionPoints.Points >= 100) { int newX = intent.Intention == IntentEnum.MoveLeft || intent.Intention == IntentEnum.MoveBottomLeft || intent.Intention == IntentEnum.MoveTopLeft ? position.Point.X - 1 : intent.Intention == IntentEnum.MoveRight || intent.Intention == IntentEnum.MoveBottomRight || intent.Intention == IntentEnum.MoveTopRight ? position.Point.X + 1 : position.Point.X; int newY = intent.Intention == IntentEnum.MoveDown || intent.Intention == IntentEnum.MoveBottomLeft || intent.Intention == IntentEnum.MoveBottomRight ? position.Point.Y - 1 : intent.Intention == IntentEnum.MoveUp || intent.Intention == IntentEnum.MoveTopLeft || intent.Intention == IntentEnum.MoveTopRight ? position.Point.Y + 1 : position.Point.Y; IEntity worldEntity = namelessGame.TimelineEntity; IWorldProvider worldProvider = null; if (worldEntity != null) { worldProvider = worldEntity.GetComponentOfType <TimeLine>().CurrentTimelineLayer.Chunks; } Tile tileToMoveTo = worldProvider.GetTile(newX, newY); IEntity entityThatOccupiedTile = null; foreach (IEntity tileEntity in tileToMoveTo.GetEntities()) { OccupiesTile occupiesTile = tileEntity.GetComponentOfType <OccupiesTile>(); if (occupiesTile != null) { entityThatOccupiedTile = tileEntity; break; } } if (entityThatOccupiedTile != null) { Door door = entityThatOccupiedTile.GetComponentOfType <Door>(); Character characterComponent = entityThatOccupiedTile.GetComponentOfType <Character>(); if (door != null) { SimpleSwitch simpleSwitch = entityThatOccupiedTile.GetComponentOfType <SimpleSwitch>(); if (simpleSwitch != null == simpleSwitch.isSwitchActive()) { entityThatOccupiedTile.GetComponentOfType <Drawable>() .Representation = 'o'; entityThatOccupiedTile.RemoveComponentOfType <BlocksVision>(); entityThatOccupiedTile.RemoveComponentOfType <OccupiesTile>(); namelessGame.Commander.EnqueueCommand( new ChangeSwitchStateCommand(simpleSwitch, false)); var ap = playerEntity.GetComponentOfType <ActionPoints>(); ap.Points -= Constants.ActionsMovementCost; // playerEntity.RemoveComponentOfType<HasTurn>(); } else { worldProvider.MoveEntity(playerEntity, new Point(newX, newY)); var ap = playerEntity.GetComponentOfType <ActionPoints>(); ap.Points -= Constants.ActionsMovementCost; } } if (characterComponent != null) { //TODO: if hostile namelessGame.Commander.EnqueueCommand(new AttackCommand(playerEntity, entityThatOccupiedTile)); var ap = playerEntity.GetComponentOfType <ActionPoints>(); ap.Points -= Constants.ActionsAttackCost; // playerEntity.RemoveComponentOfType<HasTurn>(); //TODO: do something else if friendly: chat, trade, etc } } else { worldProvider.MoveEntity(playerEntity, new Point(newX, newY)); var ap = playerEntity.GetComponentOfType <ActionPoints>(); ap.Points -= Constants.ActionsMovementCost; } } } break; case IntentEnum.LookAtMode: //InputReceiver receiver = new InputReceiver(); //Player player = entity.GetComponentOfType<Player>(); //cursor = entity.GetComponentOfType<Cursor>(); //entity.RemoveComponentOfType<InputReceiver>(); //if (player != null) //{ // IEntity cursorEntity = namelessGame.CursorEntity; // cursorEntity.AddComponent(receiver); // Drawable cursorDrawable = cursorEntity.GetComponentOfType<Drawable>(); // cursorDrawable.setVisible(true); // Position cursorPosition = cursorEntity.GetComponentOfType<Position>(); // Position playerPosition = entity.GetComponentOfType<Position>(); // cursorPosition.p.X = (playerPosition.p.X); // cursorPosition.p.Y = (playerPosition.p.Y); //} //else if (cursor != null) //{ // IEntity playerEntity = namelessGame.PlayerEntity; // playerEntity.AddComponent(receiver); // Drawable cursorDrawable = entity.GetComponentOfType<Drawable>(); // cursorDrawable.setVisible(false); //} break; case IntentEnum.PickUpItem: { var actionPoints = playerEntity.GetComponentOfType <ActionPoints>(); if (actionPoints.Points >= 100) { IEntity worldEntity = namelessGame.TimelineEntity; IWorldProvider worldProvider = null; if (worldEntity != null) { worldProvider = worldEntity.GetComponentOfType <TimeLine>().CurrentTimelineLayer .Chunks; } var position = playerEntity.GetComponentOfType <Position>(); var itemHolder = playerEntity.GetComponentOfType <ItemsHolder>(); var tile = worldProvider.GetTile(position.Point.X, position.Point.Y); List <IEntity> itemsToPickUp = new List <IEntity>(); foreach (var entityOnTIle in tile.GetEntities()) { var itemComponent = entityOnTIle.GetComponentOfType <Item>(); if (itemComponent != null) { itemsToPickUp.Add(entityOnTIle); } } if (itemsToPickUp.Any()) { if (itemsToPickUp.Count > 1) { namelessGame.ContextToSwitch = ContextFactory.GetPickUpItemContext(namelessGame); UiFactory.PickUpItemsScreen.FillItems(namelessGame); if (UiFactory.PickUpItemsScreen.ItemsTable.Items.Any()) { UiFactory.PickUpItemsScreen.ItemsTable.SelectedIndex = 0; } } else { StringBuilder builder = new StringBuilder(); var itemsCommand = new PickUpItemCommand(itemsToPickUp, itemHolder, position.Point); namelessGame.Commander.EnqueueCommand(itemsCommand); foreach (var entity1 in itemsToPickUp) { var desc = entity1.GetComponentOfType <Description>(); if (desc != null) { builder.Append($"Picked up: {desc.Name} \n"); } } var logCommand = new HudLogMessageCommand(); logCommand.LogMessage += builder.ToString(); namelessGame.Commander.EnqueueCommand(logCommand); } var ap = playerEntity.GetComponentOfType <ActionPoints>(); ap.Points -= Constants.ActionsPickUpCost; //playerEntity.RemoveComponentOfType<HasTurn>(); } } break; } case IntentEnum.SkipTurn: { var actionPoints = playerEntity.GetComponentOfType <ActionPoints>(); if (actionPoints.Points >= 100) { var ap = entity.GetComponentOfType <ActionPoints>(); ap.Points -= Constants.ActionsMovementCost; var logCommand = new HudLogMessageCommand(); logCommand.LogMessage += "Waiting"; namelessGame.Commander.EnqueueCommand(logCommand); // playerEntity.RemoveComponentOfType<HasTurn>(); } } break; case IntentEnum.Quicksave: namelessGame.ScheduleSave(); break; case IntentEnum.Quickload: namelessGame.ScheduleLoad(); break; case IntentEnum.ZoomIn: var zoomCommand = new ZoomCommand(false); namelessGame.Commander.EnqueueCommand(zoomCommand); break; case IntentEnum.ZoomOut: var zoomOutCommand = new ZoomCommand(); namelessGame.Commander.EnqueueCommand(zoomOutCommand); break; default: break; } } inputComponent.Intents.Clear(); } } }
public static void Initialize(MiLightGateway miLightGateway) { //Sonoff Switches ChristmasLights = new SonoffSwitch("192.168.10.84"); KitchenLights = new SonoffSwitch("192.168.10.81"); GlobusLight = new SonoffSwitch("192.168.10.81"); //TODO IP BedLightBalls = new SonoffSwitch("192.168.10.81"); //TODO IP //Yee Light Devices LightHallway = new YeelightDevice("192.168.10.77", "0x000000000361df8b", false, 0, "mono"); StripeA = new YeelightDevice("192.168.10.75", "0x000000000361afc3", false, 0, "stripe"); StripeB = new YeelightDevice("192.168.10.74", "0x0000000004555e6d", false, 0, "stripe"); //Mi Light Devices BulbA = new MiBulb(miLightGateway, 1); BulbB = new MiBulb(miLightGateway, 2); BulbC = new MiBulb(miLightGateway, 3); //Mi Home Devices DoorBathroom = new DoorWindowSensor("158d0001e037e5") { CustomName = "Badezimmertür" }; ApartmentDoor = new DoorWindowSensor("158d0001a5db48") { CustomName = "Wohnungstür" }; SwitchA = new SimpleSwitch("158d00019dbac7") { CustomName = "Switch A" }; SwitchB = new SimpleSwitch("158d00016da2ed") { CustomName = "Switch B" }; MotionBathroom = new MotionSensor("158d0001d87940") { CustomName = "Motion Bathroom" }; MotionHall = new MotionSensor("158d0001d47c2f") { CustomName = "Motion Hall" }; MotionLivingRoom = new MotionSensor("158d00014dc328") { CustomName = "Motion Living Room" }; SwitchC = new SensorSwitch("158d0001b86fed") { CustomName = "Switch C" }; SwitchD = new SensorSwitch("158d0001b86f59") { CustomName = "SwitchD" }; WaterLeakBathroom = new WaterLeakSensor("158d0001bb89e9") { CustomName = "Water Leak" }; TempSensorLivingRoom = new TempSensor("158d0001b7edbb") { CustomName = "Temperatur Wohnzimmer" }; SwitchE = new DoubleKeySwitch("158d0001718576") { CustomName = "Switch Wandregal" }; }
/// <summary> /// /// </summary> /// <param name="updatedObject"></param> void CreateOrUpdateSubscribedDevice(object updatedObject) { string topicString = ""; Debug.WriteLine(updatedObject.GetType().Name); switch (updatedObject.GetType().Name) { case "SubscribedLabel": SubscribedLabel updatedLabel = updatedObject as SubscribedLabel; SubscribedLabel foundedLabel = subscribedLabels.Find(x => x.Label == updatedLabel.Label); topicString = updatedLabel.Topic; break; case "SubscribedSwitch": SubscribedSwitch updatedSwitch = updatedObject as SubscribedSwitch; SubscribedSwitch foundedSwitch = subscribedSwitches.Find(x => x.SimpleSwitch == updatedSwitch.SimpleSwitch); topicString = foundedSwitch.Topic; if (!foundedSwitch.Clickable) { SimpleSwitch simpleSwitch = foundedSwitch.SimpleSwitch; Grid grid = simpleSwitch.Content as Grid; Image icon = grid.Children[1] as Image; icon.InputBindings[0].Command = null; } else { SimpleSwitch simpleSwitch = foundedSwitch.SimpleSwitch; Grid grid = simpleSwitch.Content as Grid; Image icon = grid.Children[1] as Image; Binding commandBinding = new Binding("SwitchClickedCommand"); BindingOperations.SetBinding(icon.InputBindings[0], InputBinding.CommandProperty, commandBinding); } break; case "SubscribedCamera": SubscribedCamera updatedCamera = updatedObject as SubscribedCamera; SubscribedCamera foundedCamera = subscribedCameras.Find(x => x.Camera == updatedCamera.Camera); topicString = foundedCamera.Topic; break; default: break; } if (topicString != "" && !subscribedStrings.Contains(topicString)) { subscribedStrings.Add(topicString); } if (subscribedStrings.Count != 0) { byte[] qosArray = new byte[subscribedStrings.Count]; Array.Fill(qosArray, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE); client.Subscribe(subscribedStrings.ToArray(), qosArray); } }